@ssv/ngx.command 3.0.0-dev.1 → 3.0.0-dev.1-dev.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,215 +1,215 @@
1
- import { BehaviorSubject, EMPTY } from "rxjs";
2
-
3
- import { Command } from "./command";
4
-
5
- describe("CommandSpecs", () => {
6
- let SUT: Command;
7
- let executeFn: jest.Mock<void, unknown[], unknown> ;
8
- // let executeSpyFn: jest.SpyInstance<void, unknown[], unknown>;
9
-
10
- beforeEach(() => {
11
- executeFn = jest.fn();
12
- // executeSpyFn = executeFn;
13
- });
14
-
15
- describe("given a command without canExecute$ param", () => {
16
- beforeEach(() => {
17
- SUT = new Command(executeFn);
18
- // executeSpyFn = jest.spyOn(SUT, "execute");
19
- });
20
-
21
- describe("when command is initialized", () => {
22
- it("should have canExecute set to true", () => {
23
- expect(SUT.canExecute).toBe(true);
24
- });
25
-
26
- it("should have canExecute$ set to true", done => {
27
- SUT.canExecute$.subscribe(x => {
28
- expect(x).toBe(true);
29
- done();
30
- });
31
- });
32
- });
33
-
34
- describe("when execute is invoked", () => {
35
- beforeEach(() => {
36
- SUT.execute();
37
- });
38
-
39
- it("should have isExecuting set to false after execute finishes", () => {
40
- expect(SUT.isExecuting).toBe(false);
41
- });
42
-
43
- it("should have canExecute set to true after execute finishes", () => {
44
- expect(SUT.canExecute).toBe(true);
45
- });
46
-
47
- it("should invoke execute function", () => {
48
- expect(executeFn).toHaveBeenCalledTimes(1);
49
- });
50
- });
51
- });
52
-
53
- describe("given execute is invoked", () => {
54
- describe("when canExecute is true", () => {
55
- beforeEach(() => {
56
- const isInitialValid = true;
57
- SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
58
- });
59
-
60
- it("should invoke execute function passed", () => {
61
- SUT.execute();
62
- expect(executeFn).toHaveBeenCalledTimes(1);
63
- });
64
- });
65
-
66
- describe("when observable completes", () => {
67
- beforeEach(() => {
68
- const isInitialValid = true;
69
- executeFn = jest.fn().mockImplementation(() => EMPTY);
70
- SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
71
- });
72
-
73
- it("should invoke multiple times", () => {
74
- SUT.execute();
75
- SUT.execute();
76
- expect(SUT.isExecuting).toBeFalsy();
77
- expect(executeFn).toHaveBeenCalledTimes(2);
78
- });
79
- });
80
-
81
- describe("when an error is thrown", () => {
82
- const _errorFn = console.error;
83
- beforeAll(() => {
84
- console.error = jest.fn();
85
- });
86
-
87
- beforeEach(() => {
88
- const isInitialValid = true;
89
- executeFn = jest.fn().mockImplementation(() => {
90
- throw new Error("Execution failed!");
91
- });
92
- SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
93
- });
94
-
95
- it("should invoke multiple times", () => {
96
- SUT.execute();
97
- SUT.execute();
98
- expect(SUT.isExecuting).toBeFalsy();
99
- expect(executeFn).toHaveBeenCalledTimes(2);
100
- });
101
-
102
- afterAll(() => {
103
- console.error = _errorFn;
104
- });
105
- });
106
-
107
- describe("when args are passed", () => {
108
- beforeEach(() => {
109
- const isInitialValid = true;
110
- SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
111
- });
112
-
113
- it("and has 1 param should receive 1 arg", () => {
114
- const args = { name: "rexxar" };
115
- SUT.execute(args);
116
- expect(executeFn).toHaveBeenCalledTimes(1);
117
- expect(executeFn).toHaveBeenCalledWith(args);
118
- });
119
-
120
- it("and is array param should not spread", () => {
121
- const hero = { name: "rexxar" };
122
- const args = [hero, "yello"];
123
- SUT.execute(args);
124
- expect(executeFn).toHaveBeenCalledTimes(1);
125
- expect(executeFn).toHaveBeenCalledWith([hero, "yello"]);
126
- });
127
-
128
- it("and multi args are pass should receive all", () => {
129
- const hero = { name: "rexxar" };
130
- SUT.execute(hero, "yello");
131
- expect(executeFn).toHaveBeenCalledTimes(1);
132
- expect(executeFn).toHaveBeenCalledWith(hero, "yello");
133
- });
134
- });
135
-
136
- describe("when canExecute is false", () => {
137
- beforeEach(() => {
138
- const isInitialValid = false;
139
- SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
140
- });
141
-
142
- it("should not execute the provided execute function", () => {
143
- SUT.execute();
144
- expect(executeFn).not.toHaveBeenCalled();
145
- });
146
- });
147
- });
148
-
149
- describe("given canExecute with an initial value of true", () => {
150
- let canExecute$: BehaviorSubject<boolean>;
151
-
152
- beforeEach(() => {
153
- const isInitialValid = true;
154
- canExecute$ = new BehaviorSubject<boolean>(isInitialValid);
155
- SUT = new Command(executeFn, canExecute$);
156
- });
157
-
158
- it("should have canExecute set to true", () => {
159
- expect(SUT.canExecute).toBe(true);
160
- });
161
-
162
- it("should have canExecute$ set to true", done => {
163
- SUT.canExecute$.subscribe(x => {
164
- expect(x).toBe(true);
165
- done();
166
- });
167
- });
168
-
169
- describe("when the canExecute observable changes", () => {
170
- beforeEach(() => {
171
- canExecute$.next(false);
172
- });
173
-
174
- it("should update canExecute", () => {
175
- expect(SUT.canExecute).toBe(false);
176
- });
177
-
178
- it("should update canExecute$", done => {
179
- SUT.canExecute$.subscribe(x => {
180
- expect(x).toBe(false);
181
- done();
182
- });
183
- });
184
- });
185
- });
186
-
187
- describe("given canExecute with an initial value of false", () => {
188
- beforeEach(() => {
189
- const isInitialValid = false;
190
- SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
191
- });
192
-
193
- it("should have canExecute set to false", () => {
194
- expect(SUT.canExecute).toBe(false);
195
- });
196
-
197
- it("should have canExecute$ set to false", done => {
198
- SUT.canExecute$.subscribe(x => {
199
- expect(x).toBe(false);
200
- done();
201
- });
202
- });
203
- });
204
-
205
- describe("given destroy is invoked", () => {
206
- beforeEach(() => {
207
- const isInitialValid = false;
208
- SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
209
- });
210
-
211
- it("should destroy successfully", () => {
212
- SUT.destroy();
213
- });
214
- });
215
- });
1
+ import { BehaviorSubject, EMPTY } from "rxjs";
2
+
3
+ import { Command } from "./command";
4
+
5
+ describe("CommandSpecs", () => {
6
+ let SUT: Command;
7
+ let executeFn: jest.Mock<void, unknown[], unknown> ;
8
+ // let executeSpyFn: jest.SpyInstance<void, unknown[], unknown>;
9
+
10
+ beforeEach(() => {
11
+ executeFn = jest.fn();
12
+ // executeSpyFn = executeFn;
13
+ });
14
+
15
+ describe("given a command without canExecute$ param", () => {
16
+ beforeEach(() => {
17
+ SUT = new Command(executeFn);
18
+ // executeSpyFn = jest.spyOn(SUT, "execute");
19
+ });
20
+
21
+ describe("when command is initialized", () => {
22
+ it("should have canExecute set to true", () => {
23
+ expect(SUT.canExecute).toBe(true);
24
+ });
25
+
26
+ it("should have canExecute$ set to true", done => {
27
+ SUT.canExecute$.subscribe(x => {
28
+ expect(x).toBe(true);
29
+ done();
30
+ });
31
+ });
32
+ });
33
+
34
+ describe("when execute is invoked", () => {
35
+ beforeEach(() => {
36
+ SUT.execute();
37
+ });
38
+
39
+ it("should have isExecuting set to false after execute finishes", () => {
40
+ expect(SUT.isExecuting).toBe(false);
41
+ });
42
+
43
+ it("should have canExecute set to true after execute finishes", () => {
44
+ expect(SUT.canExecute).toBe(true);
45
+ });
46
+
47
+ it("should invoke execute function", () => {
48
+ expect(executeFn).toHaveBeenCalledTimes(1);
49
+ });
50
+ });
51
+ });
52
+
53
+ describe("given execute is invoked", () => {
54
+ describe("when canExecute is true", () => {
55
+ beforeEach(() => {
56
+ const isInitialValid = true;
57
+ SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
58
+ });
59
+
60
+ it("should invoke execute function passed", () => {
61
+ SUT.execute();
62
+ expect(executeFn).toHaveBeenCalledTimes(1);
63
+ });
64
+ });
65
+
66
+ describe("when observable completes", () => {
67
+ beforeEach(() => {
68
+ const isInitialValid = true;
69
+ executeFn = jest.fn().mockImplementation(() => EMPTY);
70
+ SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
71
+ });
72
+
73
+ it("should invoke multiple times", () => {
74
+ SUT.execute();
75
+ SUT.execute();
76
+ expect(SUT.isExecuting).toBeFalsy();
77
+ expect(executeFn).toHaveBeenCalledTimes(2);
78
+ });
79
+ });
80
+
81
+ describe("when an error is thrown", () => {
82
+ const _errorFn = console.error;
83
+ beforeAll(() => {
84
+ console.error = jest.fn();
85
+ });
86
+
87
+ beforeEach(() => {
88
+ const isInitialValid = true;
89
+ executeFn = jest.fn().mockImplementation(() => {
90
+ throw new Error("Execution failed!");
91
+ });
92
+ SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
93
+ });
94
+
95
+ it("should invoke multiple times", () => {
96
+ SUT.execute();
97
+ SUT.execute();
98
+ expect(SUT.isExecuting).toBeFalsy();
99
+ expect(executeFn).toHaveBeenCalledTimes(2);
100
+ });
101
+
102
+ afterAll(() => {
103
+ console.error = _errorFn;
104
+ });
105
+ });
106
+
107
+ describe("when args are passed", () => {
108
+ beforeEach(() => {
109
+ const isInitialValid = true;
110
+ SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
111
+ });
112
+
113
+ it("and has 1 param should receive 1 arg", () => {
114
+ const args = { name: "rexxar" };
115
+ SUT.execute(args);
116
+ expect(executeFn).toHaveBeenCalledTimes(1);
117
+ expect(executeFn).toHaveBeenCalledWith(args);
118
+ });
119
+
120
+ it("and is array param should not spread", () => {
121
+ const hero = { name: "rexxar" };
122
+ const args = [hero, "yello"];
123
+ SUT.execute(args);
124
+ expect(executeFn).toHaveBeenCalledTimes(1);
125
+ expect(executeFn).toHaveBeenCalledWith([hero, "yello"]);
126
+ });
127
+
128
+ it("and multi args are pass should receive all", () => {
129
+ const hero = { name: "rexxar" };
130
+ SUT.execute(hero, "yello");
131
+ expect(executeFn).toHaveBeenCalledTimes(1);
132
+ expect(executeFn).toHaveBeenCalledWith(hero, "yello");
133
+ });
134
+ });
135
+
136
+ describe("when canExecute is false", () => {
137
+ beforeEach(() => {
138
+ const isInitialValid = false;
139
+ SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
140
+ });
141
+
142
+ it("should not execute the provided execute function", () => {
143
+ SUT.execute();
144
+ expect(executeFn).not.toHaveBeenCalled();
145
+ });
146
+ });
147
+ });
148
+
149
+ describe("given canExecute with an initial value of true", () => {
150
+ let canExecute$: BehaviorSubject<boolean>;
151
+
152
+ beforeEach(() => {
153
+ const isInitialValid = true;
154
+ canExecute$ = new BehaviorSubject<boolean>(isInitialValid);
155
+ SUT = new Command(executeFn, canExecute$);
156
+ });
157
+
158
+ it("should have canExecute set to true", () => {
159
+ expect(SUT.canExecute).toBe(true);
160
+ });
161
+
162
+ it("should have canExecute$ set to true", done => {
163
+ SUT.canExecute$.subscribe(x => {
164
+ expect(x).toBe(true);
165
+ done();
166
+ });
167
+ });
168
+
169
+ describe("when the canExecute observable changes", () => {
170
+ beforeEach(() => {
171
+ canExecute$.next(false);
172
+ });
173
+
174
+ it("should update canExecute", () => {
175
+ expect(SUT.canExecute).toBe(false);
176
+ });
177
+
178
+ it("should update canExecute$", done => {
179
+ SUT.canExecute$.subscribe(x => {
180
+ expect(x).toBe(false);
181
+ done();
182
+ });
183
+ });
184
+ });
185
+ });
186
+
187
+ describe("given canExecute with an initial value of false", () => {
188
+ beforeEach(() => {
189
+ const isInitialValid = false;
190
+ SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
191
+ });
192
+
193
+ it("should have canExecute set to false", () => {
194
+ expect(SUT.canExecute).toBe(false);
195
+ });
196
+
197
+ it("should have canExecute$ set to false", done => {
198
+ SUT.canExecute$.subscribe(x => {
199
+ expect(x).toBe(false);
200
+ done();
201
+ });
202
+ });
203
+ });
204
+
205
+ describe("given destroy is invoked", () => {
206
+ beforeEach(() => {
207
+ const isInitialValid = false;
208
+ SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
209
+ });
210
+
211
+ it("should destroy successfully", () => {
212
+ SUT.destroy();
213
+ });
214
+ });
215
+ });