-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
test-component.js
157 lines (138 loc) · 4.46 KB
/
test-component.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* 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 * as Preact from '../../../../src/preact';
import {Twitter} from '../component';
import {WithAmpContext} from '../../../../src/preact/context';
import {createRef} from '../../../../src/preact';
import {mount} from 'enzyme';
import {serializeMessage} from '../../../../src/3p-frame-messaging';
import {waitFor} from '../../../../testing/test-helper';
describes.sandboxed('Twitter preact component v1.0', {}, (env) => {
it('should render', () => {
const wrapper = mount(
<Twitter
tweetid="1356304203044499462"
style={{
'width': '500px',
'height': '600px',
}}
/>
);
const iframe = wrapper.find('iframe');
expect(iframe.prop('src')).to.equal(
'http://ads.localhost:9876/dist.3p/current/frame.max.html'
);
expect(wrapper.find('iframe').prop('style').width).to.equal('100%');
expect(wrapper.find('iframe').prop('style').height).to.equal('100%');
});
it('should call given requestResize', () => {
const requestResizeSpy = env.sandbox.spy();
const wrapper = mount(
<Twitter
tweetid="1356304203044499462"
style={{
'width': '500px',
'height': '600px',
}}
requestResize={requestResizeSpy}
/>
);
const iframe = wrapper.find('iframe').getDOMNode();
const sentinel = JSON.parse(iframe.getAttribute('name'))['attributes'][
'sentinel'
];
const mockEvent = new CustomEvent('message');
mockEvent.data = serializeMessage('embed-size', sentinel, {
'height': 1000,
});
mockEvent.source = wrapper
.getDOMNode()
.querySelector('iframe').contentWindow;
window.dispatchEvent(mockEvent);
expect(requestResizeSpy).to.have.been.calledOnce;
});
it('should change height', async () => {
const wrapper = mount(
<Twitter
tweetid="1356304203044499462"
style={{
'width': '500px',
'height': '600px',
}}
/>
);
const iframe = wrapper.find('iframe').getDOMNode();
const sentinel = JSON.parse(iframe.getAttribute('name'))['attributes'][
'sentinel'
];
const mockEvent = new CustomEvent('message');
mockEvent.data = serializeMessage('embed-size', sentinel, {
'height': 1000,
});
mockEvent.source = wrapper
.getDOMNode()
.querySelector('iframe').contentWindow;
window.dispatchEvent(mockEvent);
wrapper.update();
await waitFor(
() => wrapper.find('div').at(0).prop('style').height == 1000,
'Height changes'
);
expect(wrapper.find('div').at(0).prop('style').height).to.equal(1000);
});
it('should dispatch load event', async () => {
const ref = createRef();
const onReadyState = env.sandbox.spy();
const wrapper = mount(
<Twitter
ref={ref}
tweetid="1356304203044499462"
style={{
'width': '500px',
'height': '600px',
}}
onReadyState={onReadyState}
/>
);
let api = ref.current;
expect(api.readyState).to.equal('loading');
expect(onReadyState).to.not.be.called;
await wrapper.find('iframe').invoke('onLoad')();
api = ref.current;
expect(api.readyState).to.equal('complete');
expect(onReadyState).to.be.calledOnce.calledWith('complete');
});
it('should reset iframe on pause', () => {
const ref = createRef();
const wrapper = mount(
<WithAmpContext playable={true}>
<Twitter
ref={ref}
tweetid="1356304203044499462"
style={{
'width': '500px',
'height': '600px',
}}
/>
</WithAmpContext>
);
expect(wrapper.find('iframe')).to.have.lengthOf(1);
const iframe = wrapper.find('iframe').getDOMNode();
const spy = env.sandbox.spy(iframe, 'src', ['set']);
wrapper.setProps({playable: false});
expect(spy.set).to.be.calledOnce;
});
});