This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathOTSession.spec.js
130 lines (106 loc) · 3.63 KB
/
OTSession.spec.js
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
import React from 'react';
import { mount } from 'enzyme';
import OTSession from '../src/OTSession';
const MyComponent = () => <div />;
describe('OTSession', () => {
let session;
let wrapper;
let apiKey;
let sessionId;
let token;
beforeEach(() => {
session = jasmine.createSpyObj('session', [
'on',
'off',
'connect',
'disconnect',
]);
apiKey = 'fakeApiKey';
sessionId = 'fakeSessionId';
token = 'fakeToken';
spyOn(OT, 'initSession').and.returnValue(session);
});
describe('missing credentials', () => {
it('should throw when missing apiKey', () => {
expect(() => {
mount(<OTSession />);
}).toThrowError(/Missing apiKey/);
});
it('should throw when missing sessionId', () => {
expect(() => {
mount(<OTSession apiKey={apiKey} />);
}).toThrowError(/Missing sessionId/);
});
it('should throw when missing token', () => {
expect(() => {
mount(<OTSession apiKey={apiKey} sessionId={sessionId} />);
}).toThrowError(/Missing token/);
});
});
describe('without event handlers', () => {
describe('without children', () => {
beforeEach(() => {
wrapper = mount((
<OTSession apiKey={apiKey} sessionId={sessionId} token={token} />
));
});
it('should have a streams state', () => {
expect(wrapper.state('streams')).toEqual(jasmine.any(Array));
});
it('should init and connect to a session', () => {
expect(OT.initSession).toHaveBeenCalled();
expect(session.on).toHaveBeenCalled();
expect(session.on.calls.count()).toBe(1);
expect(session.connect).toHaveBeenCalled();
});
it('should only call on once with one argument', () => {
expect(session.on).toHaveBeenCalled();
expect(session.on.calls.count()).toBe(1);
const { args } = session.on.calls.first();
expect(args.length).toBe(1);
expect(args[0]).toEqual(jasmine.any(Object));
});
it('should update streams state when streamCreated triggered', () => {
const { args } = session.on.calls.first();
const onStreamCreated = args[0].streamCreated;
expect(onStreamCreated).toEqual(jasmine.any(Function));
const stream = { id: 'fakeStreamId' };
onStreamCreated({ stream });
expect(wrapper.state('streams')).toEqual([stream]);
});
it('should update streams state when streamDestroyed triggered', () => {
const { args } = session.on.calls.first();
const onStreamDestroyed = args[0].streamDestroyed;
expect(onStreamDestroyed).toEqual(jasmine.any(Function));
const stream = { id: 'fakeStreamId' };
wrapper.state('streams').push(stream);
expect(wrapper.state('streams')).toEqual([stream]);
onStreamDestroyed({ stream });
expect(wrapper.state('streams')).toEqual([]);
});
it('should call disconnect on umount', () => {
expect(session.disconnect).not.toHaveBeenCalled();
wrapper.unmount();
expect(session.disconnect).toHaveBeenCalled();
});
it('should have no children', () => {
expect(wrapper.children().length).toBe(0);
});
});
describe('with children', () => {
beforeEach(() => {
wrapper = mount((
<OTSession apiKey={apiKey} sessionId={sessionId} token={token}>
<MyComponent />
<MyComponent />
</OTSession>
));
});
it('should have two children', () => {
expect(wrapper.children().length).toBe(2);
});
});
});
describe('with event handlers', () => {
});
});