Skip to content

Commit 979666a

Browse files
author
Martynas Žilinskas
committed
Added actions to register and unregister actions on FormStoresHandler.
1 parent 418bed3 commit 979666a

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

packages/simplr-forms-core/__tests__/stores/form-stores-handler.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { FSHContainer, FormStoresHandlerClass } from "../../src/stores/form-stores-handler";
2+
import * as Actions from "../../src/actions/form-stores-handler-actions";
3+
import * as sinon from "Sinon";
24

35
describe("Form stores handler", () => {
46
it("returns next store unique formId", () => {
@@ -84,4 +86,26 @@ describe("Form stores handler", () => {
8486

8587
expect(storesHandler.GetStore(generatedFormId)).toBeTruthy();
8688
});
89+
90+
it("emits register action when registering a new form", () => {
91+
const storesHandler = new FormStoresHandlerClass();
92+
const callbackSpy = sinon.spy();
93+
94+
storesHandler.addListener(Actions.FormRegistered, callbackSpy);
95+
storesHandler.RegisterForm("some-kind-of-id");
96+
97+
expect(callbackSpy.called).toBe(true);
98+
});
99+
100+
fit("emits unregister action when unregistering a form", () => {
101+
const storesHandler = new FormStoresHandlerClass();
102+
const formId = "form-id";
103+
const callbackSpy = sinon.spy();
104+
105+
storesHandler.RegisterForm(formId);
106+
storesHandler.addListener(Actions.FormUnregistered, callbackSpy);
107+
storesHandler.UnregisterForm(formId);
108+
109+
expect(callbackSpy.called).toBe(true);
110+
});
87111
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class FormRegistered {
2+
constructor(private formId: string) { }
3+
4+
public get FormId() {
5+
return this.formId;
6+
}
7+
}
8+
9+
export class FormUnregistered {
10+
constructor(private formId: string) { }
11+
12+
public get FormId() {
13+
return this.formId;
14+
}
15+
}

packages/simplr-forms-core/src/stores/form-stores-handler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import * as Immutable from "immutable";
2+
import { ActionEmitter } from "action-emitter";
3+
24
import { FormStore } from "./form-store";
5+
import * as Actions from "../actions/form-stores-handler-actions";
36

4-
export class FormStoresHandlerClass {
7+
export class FormStoresHandlerClass extends ActionEmitter {
58
private storesCount: number;
69
private formStores: Immutable.Map<string, FormStore>;
710

811
constructor() {
12+
super();
913
this.resetFormStores();
1014
}
1115

@@ -65,6 +69,8 @@ export class FormStoresHandlerClass {
6569
// Add instance to formStores map by its id
6670
this.formStores = this.formStores.set(formId, storeInstance);
6771

72+
this.emit(new Actions.FormRegistered(formId));
73+
6874
return formId;
6975
}
7076

@@ -79,6 +85,7 @@ export class FormStoresHandlerClass {
7985
const store = this.formStores.get(formId);
8086
if (store != null) {
8187
this.formStores = this.formStores.delete(formId);
88+
this.emit(new Actions.FormUnregistered(formId));
8289
}
8390
}
8491

0 commit comments

Comments
 (0)