-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
101 lines (68 loc) · 2.84 KB
/
index.test.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
import { BehaviorSubject } from 'rxjs';
import { getSubject, SubjectsStorage, createPubsub } from './index';
import { act, renderHook } from '@testing-library/react'
type State = {
counter: number
};
let initialState: State;
let storage: SubjectsStorage<State>;
beforeEach(() => {
initialState = { counter: 0 };
storage = {} as SubjectsStorage<State>;
});
describe('unittests', () => {
test('getSubject creates property in storage', () => {
const subject = getSubject('counter', initialState, storage);
expect(subject).toBeInstanceOf(BehaviorSubject);
expect(storage.counter).toBe(subject);
});
test('usePub with single parameter returns callback to set value', () => {
const pubsub = createPubsub(initialState, storage);
const hook = renderHook(() => pubsub.usePub('counter'));
act(() => hook.result.current(42));
expect(storage.counter.getValue()).toEqual(42);
});
test('usePub with two parameters returns callback without arguments', () => {
const pubsub = createPubsub(initialState, storage);
const hook = renderHook(() => pubsub.usePub('counter', x => x + 34));
act(() => hook.result.current());
expect(storage.counter.getValue()).toEqual(34);
});
test('useSub returns default value', () => {
const pubsub = createPubsub(initialState, storage);
const hook = renderHook(() => pubsub.useSub('counter'));
expect(hook.result.current).toEqual(0);
});
test('useSub returns updated value', () => {
const pubsub = createPubsub(initialState, storage);
const hook = renderHook(() => pubsub.useSub('counter'));
act(() => storage.counter.next(12));
expect(hook.result.current).toEqual(12);
});
});
describe('acceptance tests', () => {
test('useSub + usPub: increment', async () => {
const pubsub = createPubsub(initialState);
const useSub = renderHook(() => pubsub.useSub('counter'));
const usePub = renderHook(() => pubsub.usePub('counter', x => x + 1));
expect(useSub.result.current).toEqual(0);
act(() => usePub.result.current());
expect(useSub.result.current).toEqual(1);
});
test('useSub + usPub: set value in callback', async () => {
const pubsub = createPubsub(initialState);
const useSub = renderHook(() => pubsub.useSub('counter'));
const usePub = renderHook(() => pubsub.usePub('counter'));
expect(useSub.result.current).toEqual(0);
act(() => usePub.result.current(34));
expect(useSub.result.current).toEqual(34);
});
test('useSub + usPub: set static value', async () => {
const pubsub = createPubsub(initialState);
const useSub = renderHook(() => pubsub.useSub('counter'));
const usePub = renderHook(() => pubsub.usePub('counter', () => 42));
expect(useSub.result.current).toEqual(0);
act(() => usePub.result.current());
expect(useSub.result.current).toEqual(42);
});
});