forked from SpringRoll/SpringRoll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserData.spec.js
107 lines (84 loc) · 2.92 KB
/
UserData.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
import sinon from 'sinon';
import { UserData } from './UserData';
import container from '../communication/BellhopSingleton';
document.body.addEventListener('message', t => {
console.log('called =>', t);
});
const assertThrows = async (F) => {
let didThrow = false;
try {
await F();
} catch(e) {
didThrow = true;
}
expect(didThrow).to.equal(true);
}
describe('UserData', () => {
beforeEach(() => {
container.connected = true;
});
afterEach(() => {
container.connected = false;
});
describe('read', () => {
it('Should reject if Bellhop is not connected', async () => {
container.connected = false;
await assertThrows(() => UserData.read('value'));
});
it('Should timeout if no response from Container', async () => {
await assertThrows(() => UserData.read('value'));
});
describe('data formatting', () => {
beforeEach(() => {
sinon.stub(container, 'send');
});
afterEach(() => {
container.send.restore();
});
it('should properly format, send, and receive an event', async () => {
// start the read workflow
const promise = UserData.read('test');
// make sure that the event was properly formatted before being sent over the iframe boundary
expect(container.send.calledWith('userDataRead', 'test')).to.equal(true);
// trigger the fake response from the client
container.trigger({
type: 'userDataRead',
data: 'hello'
});
// wait for UserData to receive the event
const result = await promise;
// make sure the UserData formatted it properly
expect(result).to.equal('hello');
});
});
});
describe('write', () => {
it('Should reject if Bellhop is not connected', async () => {
container.connected = false;
await assertThrows(() => UserData.write('value', { foo: 'bar' }));
});
it('Should timeout if no response from Container', async () => {
await assertThrows(() => UserData.write('name', { foo: 'bar' }));
});
});
describe('delete', () => {
it('should reject if Bellhop is not connected', async () => {
container.connected = false;
await assertThrows(() => UserData.delete('value'));
});
describe('data formatting', () => {
beforeEach(() => sinon.stub(container, 'send'));
afterEach(() => container.send.restore());
it('should properly format, send, and receive an event', async () => {
// start the delete workflow
const promise = UserData.delete('test');
// make sure that the event was properly formatted before sent over the iframe boundary
expect(container.send.calledWith('userDataRemove', 'test')).to.equal(true);
// trigger the fake response from the client
container.trigger('userDataRemove');
// make sure the promise resolves properly
await promise;
});
})
});
});