-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver.spec.ts
More file actions
57 lines (40 loc) · 1.78 KB
/
observer.spec.ts
File metadata and controls
57 lines (40 loc) · 1.78 KB
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
import { describe, it } from '@jest/globals';
import Subject from './Subject';
import Observer from './Observer';
describe('Behaviours -> Observer design pattern', () => {
beforeEach(jest.resetAllMocks);
it('Should be able to notify the observer when change has occurred', () => {
const subject: Subject = new Subject();
const observer: Observer = new Observer(subject);
jest.spyOn(observer, 'update').mockImplementation(() => jest.fn());
subject.publish();
expect(observer.update).toHaveBeenCalled();
});
it('Should be able to notify many observers when change has occurred', () => {
const subject: Subject = new Subject();
const observer: Observer = new Observer(subject);
const observer2: Observer = new Observer();
jest.spyOn(observer, 'update').mockImplementation(() => jest.fn());
jest.spyOn(observer2, 'update').mockImplementation(() => jest.fn());
subject.subscribe(observer2);
subject.publish();
expect(observer.update).toHaveBeenCalled();
expect(observer2.update).toHaveBeenCalled();
});
it('Should not be able to subscribe an observer if it is already subscribed', () => {
const subject: Subject = new Subject();
const observer: Observer = new Observer(subject);
jest.spyOn(observer, 'update').mockImplementation(() => jest.fn());
subject.subscribe(observer);
subject.publish();
expect(observer.update).toBeCalledTimes(1);
});
it('Should not be able to notify the observer when change has occurred', () => {
const subject: Subject = new Subject();
const observer: Observer = new Observer(subject);
jest.spyOn(observer, 'update').mockImplementation(() => jest.fn());
subject.unsubscribe(observer);
subject.publish();
expect(observer.update).not.toHaveBeenCalled();
});
});