-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.js
92 lines (74 loc) · 2.25 KB
/
index.test.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
/* eslint-env jest */
import lazyFB from './index.js'
describe('Lazy FB', () => {
beforeAll(() => {
document.body.appendChild(document.createElement('script'))
})
afterEach(() => {
const js = document.getElementById('facebook-jssdk')
js.parentNode.removeChild(js)
})
it('should load sdk with default options', () => {
window.FB = {
init: jest.fn()
}
// pretend the sdk has been loaded
setTimeout(() => window.fbAsyncInit())
return lazyFB().then(sdk => {
const js = document.getElementById('facebook-jssdk')
expect(sdk).toBe(window.FB)
expect(js.src).toBe('https://connect.facebook.net/en_US/sdk.js')
expect(window.FB.init).toHaveBeenCalledWith({ version: 'v2.11' })
})
})
it('should load sdk with custom options', () => {
window.FB = {
init: jest.fn()
}
// pretend the sdk has been loaded
setTimeout(() => window.fbAsyncInit())
const opts = {
lang: 'de_DE',
debug: true,
sdkModule: 'xfbml.customerchat'
}
return lazyFB(opts).then(sdk => {
const js = document.getElementById('facebook-jssdk')
expect(sdk).toBe(window.FB)
expect(js.src).toBe('https://connect.facebook.net/de_DE/sdk/xfbml.customerchat/debug.js')
expect(window.FB.init).toHaveBeenCalledWith({
version: 'v2.11',
sdkModule: 'xfbml.customerchat'
})
})
})
it('should init sdk with custom options', () => {
window.FB = {
init: jest.fn()
}
// pretend the sdk has been loaded
setTimeout(() => window.fbAsyncInit())
return lazyFB({ cookie: true, appId: 'app-id' }).then(sdk => {
expect(sdk).toBe(window.FB)
expect(window.FB.init).toHaveBeenCalledWith({
appId: 'app-id',
cookie: true,
version: 'v2.11'
})
})
})
it('should not load sdk again if it is already on the page', () => {
// insert pretend-sdk
const js = document.createElement('script')
js.id = 'facebook-jssdk'
document.body.appendChild(js)
window.FB = {
init: jest.fn()
}
return lazyFB().then(sdk => {
expect(sdk).toBe(window.FB)
expect(document.querySelectorAll('#facebook-jssdk').length).toBe(1)
expect(window.FB.init).not.toHaveBeenCalled()
})
})
})