-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-spec.ts
152 lines (124 loc) · 4.88 KB
/
test-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { marbles, cases } from "rxjs-marbles/mocha";
import { MyEffects } from './src/effects';
import { UIActions, UserActions } from './src/actions';
import { Streams } from './src/streams';
describe("signInEffect$", () => {
let testEffects: MyEffects;
beforeEach(() => {
testEffects = new MyEffects();
});
it("should create UIActions on successful login", marbles(marble => {
testEffects.mockActionStream.next(new UserActions().SignIn);
const expectedActions = {
a: new UIActions().Reset,
b: new UIActions().LoadMainPane,
c: new UIActions().AddMessage
};
const expected = marble.cold("(abc)", expectedActions);
marble.expect(testEffects.signInEffect$).toBeObservable(expected);
}));
it("should create AddErrorMessage action on SignIn failure", marbles(marble => {
testEffects.mockActionStream.error('random error');
// test that it errors out, and completes correctly
const expectedActions = {
a: new UIActions().AddErrorMessage,
};
const expected = marble.cold("(a|)", expectedActions); // NOTE: the | denotes that the observable should have completed
marble.expect(testEffects.signInEffect$).toBeObservable(expected);
// The stream should stay closed after an error scenario
testEffects.mockActionStream.next(new UserActions().SignIn);
marble.expect(testEffects.signInEffect$).toBeObservable(expected);
}));
cases("should only be triggered by SignIn actions", (marble, collateral) => {
testEffects.mockActionStream.next(collateral.action);
const expected = marble.cold(''); // nothing should be created
marble.expect(testEffects.signInEffect$).toBeObservable(expected);
}, {
"SignOut": {
action: new UserActions().SignOut,
},
"LoadMainPane": {
action: new UIActions().LoadMainPane,
}
});
});
describe('validatedNumberData$', () => {
let testStreams: Streams;
it("should double the provided stream of numbers", marbles(marble => {
const sourceData = {
a: 6,
b: 158,
c: 90
};
const expectedData = {
a: 12,
b: 316,
c: 180
};
const source = marble.cold( '--a-b--c--|', sourceData);
const expected = marble.cold( '--a-b--c--|', expectedData);
testStreams = new Streams(source);
const destination = testStreams.validatedNumberData$;
marble.expect(destination).toBeObservable(expected);
}));
it("should throw error if stream completes without providing data", marbles(marble => {
const source = marble.cold( '--|');
const expected = marble.cold('--#', undefined, 'Tried to close stream before publishing data');
testStreams = new Streams(source);
const destination = testStreams.validatedNumberData$;
marble.expect(destination).toBeObservable(expected);
}));
it("should throw unedited errors", marbles(marble => {
const sourceData = {
a: 6,
b: 158,
c: 90
};
const expectedData = {
a: 12,
b: 316,
c: 180
};
const source = marble.cold( '--a--b--c-#|', sourceData, 'do not modify, let this error message pass through' );
const expected = marble.cold('--a--b--c-#', expectedData, 'do not modify, let this error message pass through');
testStreams = new Streams(source);
const destination = testStreams.validatedNumberData$;
marble.expect(destination).toBeObservable(expected);
}));
it("should not allow odd numbers", marbles(marble => {
const sourceData = {
a: 6,
b: 159,
c: 158,
d: 90,
e: 1
};
const expectedData = {
a: 12,
b: 316,
c: 180
};
const source = marble.cold( '--a-b--c-d-e|', sourceData);
const expected = marble.cold( '--a----b-c--|', expectedData);
testStreams = new Streams(source);
const destination = testStreams.validatedNumberData$
marble.expect(destination).toBeObservable(expected);
}));
it("should only operate on number types", marbles(marble => {
const sourceData = {
a: 6,
b: 'You should play Destiny2',
c: ['a','b', 8],
d: false,
e: true
};
const expectedData = {
a: 12
};
const source = marble.cold( '--a-b--c-d-e|', sourceData);
const expected = marble.cold( '--a---------|', expectedData);
testStreams = new Streams(source);
const destination = testStreams.validatedNumberData$
marble.expect(destination).toBeObservable(expected);
}));
});