@ssv/ngx.command 2.3.2 → 3.0.0-dev.1
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.
- package/README.md +242 -242
- package/eslint.config.js +43 -0
- package/jest.config.ts +21 -0
- package/ng-package.json +7 -0
- package/package.json +2 -26
- package/project.json +36 -0
- package/src/command-ref.directive.ts +57 -0
- package/src/command.directive.ts +192 -0
- package/src/command.directive.xspec.ts +105 -0
- package/src/command.model.ts +36 -0
- package/src/command.options.ts +45 -0
- package/src/command.spec.ts +215 -0
- package/src/command.ts +170 -0
- package/src/command.util.ts +50 -0
- package/{index.d.ts → src/index.ts} +8 -8
- package/src/module.ts +17 -0
- package/src/test-setup.ts +8 -0
- package/src/version.ts +1 -0
- package/tsconfig.json +28 -0
- package/tsconfig.lib.json +17 -0
- package/tsconfig.lib.prod.json +9 -0
- package/tsconfig.spec.json +16 -0
- package/LICENSE +0 -21
- package/command-ref.directive.d.ts +0 -29
- package/command.d.ts +0 -47
- package/command.directive.d.ts +0 -25
- package/command.model.d.ts +0 -28
- package/command.util.d.ts +0 -15
- package/config.d.ts +0 -18
- package/esm2020/command-ref.directive.mjs +0 -54
- package/esm2020/command.directive.mjs +0 -166
- package/esm2020/command.mjs +0 -127
- package/esm2020/command.model.mjs +0 -2
- package/esm2020/command.util.mjs +0 -29
- package/esm2020/config.mjs +0 -8
- package/esm2020/index.mjs +0 -8
- package/esm2020/module.mjs +0 -49
- package/esm2020/ssv-ngx.command.mjs +0 -5
- package/esm2020/version.mjs +0 -2
- package/fesm2015/ssv-ngx.command.mjs +0 -419
- package/fesm2015/ssv-ngx.command.mjs.map +0 -1
- package/fesm2020/ssv-ngx.command.mjs +0 -424
- package/fesm2020/ssv-ngx.command.mjs.map +0 -1
- package/module.d.ts +0 -15
- package/version.d.ts +0 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
import { Directive, OnInit, OnDestroy, Input } from "@angular/core";
|
|
3
|
+
|
|
4
|
+
import { ICommand, CommandCreator } from "./command.model";
|
|
5
|
+
import { isCommandCreator } from "./command.util";
|
|
6
|
+
import { Command } from "./command";
|
|
7
|
+
|
|
8
|
+
const NAME_CAMEL = "ssvCommandRef";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Command creator ref, directive which allows creating Command in the template
|
|
12
|
+
* and associate it to a command (in order to share executions).
|
|
13
|
+
* @example
|
|
14
|
+
* ### Most common usage
|
|
15
|
+
* ```html
|
|
16
|
+
* <div #actionCmd="ssvCommandRef" [ssvCommandRef]="{host: this, execute: removeHero$, canExecute: isValid$}">
|
|
17
|
+
* <button [ssvCommand]="actionCmd.command" [ssvCommandParams]="hero">
|
|
18
|
+
* Remove
|
|
19
|
+
* </button>
|
|
20
|
+
* <button [ssvCommand]="actionCmd.command" [ssvCommandParams]="hero">
|
|
21
|
+
* Remove
|
|
22
|
+
* </button>
|
|
23
|
+
* </div>
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
@Directive({
|
|
28
|
+
selector: `[${NAME_CAMEL}]`,
|
|
29
|
+
exportAs: NAME_CAMEL,
|
|
30
|
+
standalone: true,
|
|
31
|
+
})
|
|
32
|
+
export class CommandRefDirective implements OnInit, OnDestroy {
|
|
33
|
+
|
|
34
|
+
@Input(NAME_CAMEL) commandCreator: CommandCreator | undefined;
|
|
35
|
+
|
|
36
|
+
get command(): ICommand { return this._command; }
|
|
37
|
+
private _command!: ICommand;
|
|
38
|
+
|
|
39
|
+
ngOnInit(): void {
|
|
40
|
+
if (isCommandCreator(this.commandCreator)) {
|
|
41
|
+
const isAsync = this.commandCreator.isAsync || this.commandCreator.isAsync === undefined;
|
|
42
|
+
|
|
43
|
+
const execFn = this.commandCreator.execute.bind(this.commandCreator.host);
|
|
44
|
+
this._command = new Command(execFn, this.commandCreator.canExecute as Observable<boolean> | undefined, isAsync);
|
|
45
|
+
} else {
|
|
46
|
+
throw new Error(`${NAME_CAMEL}: [${NAME_CAMEL}] is not defined properly!`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
ngOnDestroy(): void {
|
|
51
|
+
// console.log("[commandRef::destroy]");
|
|
52
|
+
if (this._command) {
|
|
53
|
+
this._command.destroy();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Directive,
|
|
3
|
+
OnInit,
|
|
4
|
+
OnDestroy,
|
|
5
|
+
Input,
|
|
6
|
+
HostListener,
|
|
7
|
+
ElementRef,
|
|
8
|
+
Renderer2,
|
|
9
|
+
ChangeDetectorRef,
|
|
10
|
+
inject,
|
|
11
|
+
} from "@angular/core";
|
|
12
|
+
import { Subject } from "rxjs";
|
|
13
|
+
import { tap, delay, takeUntil } from "rxjs/operators";
|
|
14
|
+
|
|
15
|
+
import { CommandOptions, COMMAND_OPTIONS } from "./command.options";
|
|
16
|
+
import { Command } from "./command";
|
|
17
|
+
import { isCommand, isCommandCreator } from "./command.util";
|
|
18
|
+
import { CommandCreator, ICommand } from "./command.model";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Controls the state of a component in sync with `Command`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ### Most common usage
|
|
25
|
+
* ```html
|
|
26
|
+
* <button [ssvCommand]="saveCmd">Save</button>
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
*
|
|
30
|
+
* ### Usage with options
|
|
31
|
+
* ```html
|
|
32
|
+
* <button [ssvCommand]="saveCmd" [ssvCommandOptions]="{executingCssClass: 'in-progress'}">Save</button>
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
*
|
|
36
|
+
* ### Usage with params
|
|
37
|
+
* This is useful for collections (loops) or using multiple actions with different args.
|
|
38
|
+
* *NOTE: This will share the `isExecuting` when used with multiple controls.*
|
|
39
|
+
*
|
|
40
|
+
* #### With single param
|
|
41
|
+
*
|
|
42
|
+
* ```html
|
|
43
|
+
* <button [ssvCommand]="saveCmd" [ssvCommandParams]="{id: 1}">Save</button>
|
|
44
|
+
* ```
|
|
45
|
+
* *NOTE: if you have only 1 argument as an array, it should be enclosed within an array e.g. `[['apple', 'banana']]`,
|
|
46
|
+
* else it will spread and you will `arg1: "apple", arg2: "banana"`*
|
|
47
|
+
*
|
|
48
|
+
* #### With multi params
|
|
49
|
+
* ```html
|
|
50
|
+
* <button [ssvCommand]="saveCmd" [ssvCommandParams]="[{id: 1}, 'hello', hero]">Save</button>
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* ### Usage with Command Creator
|
|
54
|
+
* This is useful for collections (loops) or using multiple actions with different args, whilst not sharing `isExecuting`.
|
|
55
|
+
*
|
|
56
|
+
*
|
|
57
|
+
* ```html
|
|
58
|
+
* <button [ssvCommand]="{host: this, execute: removeHero$, canExecute: isValid$, params: [hero, 1337, 'xx']}">Save</button>
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
const NAME_CAMEL = "ssvCommand";
|
|
64
|
+
|
|
65
|
+
// let nextUniqueId = 0;
|
|
66
|
+
|
|
67
|
+
@Directive({
|
|
68
|
+
selector: `[${NAME_CAMEL}]`,
|
|
69
|
+
exportAs: NAME_CAMEL,
|
|
70
|
+
standalone: true,
|
|
71
|
+
})
|
|
72
|
+
export class CommandDirective implements OnInit, OnDestroy {
|
|
73
|
+
|
|
74
|
+
// readonly id = `${NAME_CAMEL}-${nextUniqueId++}`;
|
|
75
|
+
private readonly globalOptions = inject(COMMAND_OPTIONS);
|
|
76
|
+
private readonly renderer = inject(Renderer2);
|
|
77
|
+
private readonly element = inject(ElementRef);
|
|
78
|
+
private readonly cdr = inject(ChangeDetectorRef);
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@Input(NAME_CAMEL) commandOrCreator: ICommand | CommandCreator | undefined;
|
|
82
|
+
|
|
83
|
+
@Input(`${NAME_CAMEL}Options`)
|
|
84
|
+
get commandOptions(): CommandOptions { return this._commandOptions; }
|
|
85
|
+
set commandOptions(value: Partial<CommandOptions>) {
|
|
86
|
+
if (value === this._commandOptions) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
this._commandOptions = {
|
|
90
|
+
...this.globalOptions,
|
|
91
|
+
...value,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@Input(`${NAME_CAMEL}Params`) commandParams: unknown | unknown[];
|
|
96
|
+
|
|
97
|
+
get command(): ICommand { return this._command; }
|
|
98
|
+
|
|
99
|
+
private _command!: ICommand;
|
|
100
|
+
private _commandOptions: CommandOptions = this.globalOptions;
|
|
101
|
+
private _destroy$ = new Subject<void>();
|
|
102
|
+
|
|
103
|
+
ngOnInit(): void {
|
|
104
|
+
// console.log("[ssvCommand::init]", this.config);
|
|
105
|
+
if (!this.commandOrCreator) {
|
|
106
|
+
throw new Error(`${NAME_CAMEL}: [${NAME_CAMEL}] should be defined!`);
|
|
107
|
+
} else if (isCommand(this.commandOrCreator)) {
|
|
108
|
+
this._command = this.commandOrCreator;
|
|
109
|
+
} else if (isCommandCreator(this.commandOrCreator)) {
|
|
110
|
+
const isAsync = this.commandOrCreator.isAsync || this.commandOrCreator.isAsync === undefined;
|
|
111
|
+
|
|
112
|
+
// todo: find something like this for ivy (or angular10+)
|
|
113
|
+
// const hostComponent = (this.viewContainer as any)._view.component;
|
|
114
|
+
|
|
115
|
+
const execFn = this.commandOrCreator.execute.bind(this.commandOrCreator.host);
|
|
116
|
+
this.commandParams = this.commandParams || this.commandOrCreator.params;
|
|
117
|
+
|
|
118
|
+
const canExec = this.commandOrCreator.canExecute instanceof Function
|
|
119
|
+
? this.commandOrCreator.canExecute.bind(this.commandOrCreator.host, this.commandParams)()
|
|
120
|
+
: this.commandOrCreator.canExecute;
|
|
121
|
+
|
|
122
|
+
// console.log("[ssvCommand::init] command creator", {
|
|
123
|
+
// firstParam: this.commandParams ? this.commandParams[0] : null,
|
|
124
|
+
// params: this.commandParams
|
|
125
|
+
// });
|
|
126
|
+
this._command = new Command(execFn, canExec, isAsync);
|
|
127
|
+
} else {
|
|
128
|
+
throw new Error(`${NAME_CAMEL}: [${NAME_CAMEL}] is not defined properly!`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
this._command.subscribe();
|
|
132
|
+
this._command.canExecute$.pipe(
|
|
133
|
+
this.commandOptions.hasDisabledDelay
|
|
134
|
+
? delay(1)
|
|
135
|
+
: tap(() => { /* stub */ }),
|
|
136
|
+
tap(x => {
|
|
137
|
+
this.trySetDisabled(!x);
|
|
138
|
+
// console.log("[ssvCommand::canExecute$]", { canExecute: x });
|
|
139
|
+
this.cdr.markForCheck();
|
|
140
|
+
}),
|
|
141
|
+
takeUntil(this._destroy$),
|
|
142
|
+
).subscribe();
|
|
143
|
+
|
|
144
|
+
if (this._command.isExecuting$) {
|
|
145
|
+
this._command.isExecuting$.pipe(
|
|
146
|
+
tap(x => {
|
|
147
|
+
// console.log("[ssvCommand::isExecuting$]", x, this.commandOptions);
|
|
148
|
+
if (x) {
|
|
149
|
+
this.renderer.addClass(
|
|
150
|
+
this.element.nativeElement,
|
|
151
|
+
this.commandOptions.executingCssClass
|
|
152
|
+
);
|
|
153
|
+
} else {
|
|
154
|
+
this.renderer.removeClass(
|
|
155
|
+
this.element.nativeElement,
|
|
156
|
+
this.commandOptions.executingCssClass
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
}),
|
|
160
|
+
takeUntil(this._destroy$),
|
|
161
|
+
).subscribe();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
@HostListener("click")
|
|
166
|
+
onClick(): void {
|
|
167
|
+
// console.log("[ssvCommand::onClick]", this.commandParams);
|
|
168
|
+
if (Array.isArray(this.commandParams)) {
|
|
169
|
+
this._command.execute(...this.commandParams);
|
|
170
|
+
} else {
|
|
171
|
+
this._command.execute(this.commandParams);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
ngOnDestroy(): void {
|
|
176
|
+
// console.log("[ssvCommand::destroy]");
|
|
177
|
+
this._destroy$.next();
|
|
178
|
+
this._destroy$.complete();
|
|
179
|
+
if (this._command) {
|
|
180
|
+
this._command.unsubscribe();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
private trySetDisabled(disabled: boolean) {
|
|
185
|
+
if (this.commandOptions.handleDisabled) {
|
|
186
|
+
// console.warn(">>>> disabled", { id: this.id, disabled });
|
|
187
|
+
this.renderer.setProperty(this.element.nativeElement, "disabled", disabled);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
}
|
|
192
|
+
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// import {Observable} from "rxjs/Observable";
|
|
2
|
+
// import {inject, addProviders, async} from "@angular/core/testing";
|
|
3
|
+
// import {TestComponentBuilder, ComponentFixture} from "@angular/compiler/testing";
|
|
4
|
+
// import {Component, provide} from "@angular/core";
|
|
5
|
+
|
|
6
|
+
// import {Command, ICommand, CommandDirective} from "./index";
|
|
7
|
+
|
|
8
|
+
// @Component({
|
|
9
|
+
// template: `<button class="btn" [ssvCommand]="saveCmd" >Save</button>`,
|
|
10
|
+
// directives: [
|
|
11
|
+
// CommandDirective
|
|
12
|
+
// ]
|
|
13
|
+
// })
|
|
14
|
+
// class TestContainer {
|
|
15
|
+
// saveCmd = new Command(() => Observable.of("yey").delay(2000), null, true);
|
|
16
|
+
// }
|
|
17
|
+
|
|
18
|
+
// @Component({
|
|
19
|
+
// template: `<button class="btn" [ssvCommand]="emptyCmd" >Save</button>`,
|
|
20
|
+
// directives: [
|
|
21
|
+
// CommandDirective
|
|
22
|
+
// ]
|
|
23
|
+
// })
|
|
24
|
+
// class NullCommandTestContainer {
|
|
25
|
+
|
|
26
|
+
// }
|
|
27
|
+
|
|
28
|
+
// describe("CommandDirectiveSpecs", () => {
|
|
29
|
+
|
|
30
|
+
// let testComponentBuilder: TestComponentBuilder;
|
|
31
|
+
|
|
32
|
+
// beforeEach(inject([
|
|
33
|
+
// TestComponentBuilder
|
|
34
|
+
// ], (
|
|
35
|
+
// _testComponentBuilder: TestComponentBuilder
|
|
36
|
+
// ) => {
|
|
37
|
+
// testComponentBuilder = _testComponentBuilder;
|
|
38
|
+
// })
|
|
39
|
+
// );
|
|
40
|
+
|
|
41
|
+
// describe("given an undefined command", () => {
|
|
42
|
+
// xit("should throw error", async(() => {
|
|
43
|
+
// testComponentBuilder
|
|
44
|
+
// .createAsync(NullCommandTestContainer)
|
|
45
|
+
// .then((fixture: ComponentFixture<NullCommandTestContainer>) => {
|
|
46
|
+
// expect(() => {
|
|
47
|
+
// fixture.detectChanges();
|
|
48
|
+
// }).toThrowError("[commandDirective] command should be defined!");
|
|
49
|
+
// });
|
|
50
|
+
|
|
51
|
+
// }));
|
|
52
|
+
// });
|
|
53
|
+
|
|
54
|
+
// describe("given command isExecuting is set to true", () => {
|
|
55
|
+
// xit("should have element disabled", async(() => {
|
|
56
|
+
// testComponentBuilder
|
|
57
|
+
// .createAsync(TestContainer)
|
|
58
|
+
// .then((fixture: ComponentFixture<TestContainer>) => {
|
|
59
|
+
// fixture.detectChanges();
|
|
60
|
+
// let container = fixture.componentInstance as TestContainer;
|
|
61
|
+
// let element = fixture.debugElement.nativeElement.querySelector("button");
|
|
62
|
+
// element.click();
|
|
63
|
+
// fixture.detectChanges();
|
|
64
|
+
// expect(element).toBeDisabled();
|
|
65
|
+
// });
|
|
66
|
+
// }));
|
|
67
|
+
|
|
68
|
+
// xit("should have executingCssClass added", () => {
|
|
69
|
+
|
|
70
|
+
// });
|
|
71
|
+
|
|
72
|
+
// xdescribe("when isExecuting has been finished", () => {
|
|
73
|
+
// xit("should have element enabled", () => {
|
|
74
|
+
|
|
75
|
+
// });
|
|
76
|
+
|
|
77
|
+
// xit("should have executingCssClass removed", () => {
|
|
78
|
+
|
|
79
|
+
// });
|
|
80
|
+
// });
|
|
81
|
+
// });
|
|
82
|
+
|
|
83
|
+
// xdescribe("given command canExecute is set to true", () => {
|
|
84
|
+
// xit("should have element enabled", () => {
|
|
85
|
+
|
|
86
|
+
// });
|
|
87
|
+
// });
|
|
88
|
+
|
|
89
|
+
// xdescribe("given command canExecute is set to false", () => {
|
|
90
|
+
// xit("should have element disabled", () => {
|
|
91
|
+
|
|
92
|
+
// });
|
|
93
|
+
// });
|
|
94
|
+
|
|
95
|
+
// xdescribe("given commandOptions are provided", () => {
|
|
96
|
+
// xdescribe("when isExecuting is set true", () => {
|
|
97
|
+
// xit("should have executingCssClass added with the provided option", () => {
|
|
98
|
+
|
|
99
|
+
// });
|
|
100
|
+
// });
|
|
101
|
+
|
|
102
|
+
// });
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
// });
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
|
|
3
|
+
export interface ICommand {
|
|
4
|
+
/** Determines whether the command is currently executing, as a snapshot value. */
|
|
5
|
+
readonly isExecuting: boolean;
|
|
6
|
+
/** Determines whether the command is currently executing, as an observable. */
|
|
7
|
+
readonly isExecuting$: Observable<boolean>;
|
|
8
|
+
/** Determines whether the command can execute or not, as a snapshot value. */
|
|
9
|
+
readonly canExecute: boolean;
|
|
10
|
+
/** Determines whether the command can execute or not, as an observable. */
|
|
11
|
+
readonly canExecute$: Observable<boolean>;
|
|
12
|
+
|
|
13
|
+
/** Determines whether to auto destroy when having 0 subscribers (defaults to `true`). */
|
|
14
|
+
autoDestroy: boolean;
|
|
15
|
+
|
|
16
|
+
/** Execute function to invoke. */
|
|
17
|
+
execute(...args: unknown[]): void;
|
|
18
|
+
|
|
19
|
+
/** Disposes all resources held by subscriptions. */
|
|
20
|
+
destroy(): void;
|
|
21
|
+
|
|
22
|
+
/** Subscribe listener, in order to handle auto disposing. */
|
|
23
|
+
subscribe(): void;
|
|
24
|
+
|
|
25
|
+
/** Unsubscribe listener, in order to handle auto disposing. */
|
|
26
|
+
unsubscribe(): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface CommandCreator {
|
|
30
|
+
execute: (...args: unknown[]) => Observable<unknown> | Promise<unknown> | void;
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
32
|
+
canExecute?: Observable<boolean> | Function;
|
|
33
|
+
params?: unknown | unknown[];
|
|
34
|
+
isAsync?: boolean;
|
|
35
|
+
host: unknown;
|
|
36
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { EnvironmentProviders, InjectionToken, makeEnvironmentProviders } from "@angular/core";
|
|
2
|
+
|
|
3
|
+
export interface CommandOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Css Class which gets added/removed on the Command element's host while Command `isExecuting$`.
|
|
6
|
+
*/
|
|
7
|
+
executingCssClass: string;
|
|
8
|
+
|
|
9
|
+
/** Determines whether the disabled will be handled by the directive or not.
|
|
10
|
+
* Disable handled by directive's doesn't always play nice when used with other component/pipe/directive and they also handle disabled.
|
|
11
|
+
* This disables the handling manually and need to pass explicitly `[disabled]="!saveCmd.canExecute"`.
|
|
12
|
+
*/
|
|
13
|
+
handleDisabled: boolean;
|
|
14
|
+
|
|
15
|
+
/** Determine whether to set a `delay(1)` when setting the disabled. Which might be needed when working with external
|
|
16
|
+
* components/directives (such as material button)
|
|
17
|
+
*/
|
|
18
|
+
hasDisabledDelay: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const DEFAULT_OPTIONS = Object.freeze<CommandOptions>({
|
|
22
|
+
executingCssClass: "executing",
|
|
23
|
+
handleDisabled: true,
|
|
24
|
+
hasDisabledDelay: false,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const COMMAND_OPTIONS = new InjectionToken<CommandOptions>("SSV_COMMAND_OPTIONS", {
|
|
28
|
+
factory: () => DEFAULT_OPTIONS,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export function provideSsvCommandOptions(options: CommandOptions | (() => CommandOptions)): EnvironmentProviders {
|
|
32
|
+
let opts = typeof options === "function" ? options() : options;
|
|
33
|
+
opts = opts
|
|
34
|
+
? {
|
|
35
|
+
...DEFAULT_OPTIONS,
|
|
36
|
+
...opts,
|
|
37
|
+
}
|
|
38
|
+
: DEFAULT_OPTIONS;
|
|
39
|
+
return makeEnvironmentProviders([
|
|
40
|
+
{
|
|
41
|
+
provide: DEFAULT_OPTIONS,
|
|
42
|
+
useValue: opts,
|
|
43
|
+
},
|
|
44
|
+
]);
|
|
45
|
+
}
|
|
@@ -0,0 +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
|
+
});
|