-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathnotifier.spec.ts
139 lines (118 loc) · 3.91 KB
/
notifier.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
/**
* Copyright 2019-2022, 2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { notifier } from './notifier';
describe('notifier', () => {
it('should have a subscribe method defined', () => {
expect(notifier.subscribe).toBeDefined();
});
it('should have a notify method defined', () => {
expect(notifier.notify).toBeDefined();
});
describe('Subscribing single key', () => {
let callback: jest.MockedFunction<() => void>;
const key = 'key_1';
beforeEach(() => {
callback = jest.fn();
notifier.subscribe(key, callback);
});
describe('when notify event envoked with the relevent key', () => {
beforeEach(() => {
notifier.notify(key);
});
it('should call the callback', () => {
expect(callback).toHaveBeenCalled();
});
it('should call the callback once only', () => {
expect(callback).toHaveBeenCalledTimes(1);
});
});
describe('when notify event envoked with the irrelevant key', () => {
beforeEach(() => {
notifier.notify('another_key');
});
it('should not call the callback', () => {
expect(callback).not.toHaveBeenCalled();
});
});
});
describe('Subscribing multiple key', () => {
let callback1: jest.MockedFunction<() => void>;
const key1 = 'key_1';
let callback2: jest.MockedFunction<() => void>;
const key2 = 'key_2';
beforeEach(() => {
callback1 = jest.fn();
callback2 = jest.fn();
notifier.subscribe(key1, callback1);
notifier.subscribe(key2, callback2);
});
describe('notifing particular key', () => {
beforeEach(() => {
notifier.notify(key1);
});
it('should call the callback of key 1 only', () => {
expect(callback1).toHaveBeenCalledTimes(1);
});
it('should not call the callback of key 2', () => {
expect(callback2).not.toHaveBeenCalled();
});
});
});
describe('Subscribing similar key with multiple instances', () => {
let callback1: jest.MockedFunction<() => void>;
const sameKey1 = 'key_1';
let callback2: jest.MockedFunction<() => void>;
const sameKey2 = 'key_1';
beforeEach(() => {
callback1 = jest.fn();
callback2 = jest.fn();
notifier.subscribe(sameKey1, callback1);
notifier.subscribe(sameKey2, callback2);
});
describe('when notifing the key', () => {
beforeEach(() => {
notifier.notify(sameKey1);
});
it('should call all the callbacks of particular key', () => {
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledTimes(1);
});
});
});
describe('unsubscribing the key', () => {
let callback: jest.MockedFunction<() => void>;
const key = 'key_1';
beforeEach(() => {
callback = jest.fn();
});
describe('subscribe should return a function', () => {
it('should call the callback', () => {
const unsubscribe = notifier.subscribe(key, callback);
expect(unsubscribe).toBeInstanceOf(Function);
});
});
describe('should not envoke callback on notify if is unsubscribed', () => {
beforeEach(() => {
const unsubscribe = notifier.subscribe(key, callback);
unsubscribe();
notifier.notify(key);
});
it('should not call the callback', () => {
expect(callback).not.toHaveBeenCalled();
});
});
});
});