-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
188 lines (158 loc) · 4.47 KB
/
index.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* External dependencies
*/
import { shallow } from 'enzyme';
/**
* Internal dependencies
*/
import { PostPreviewButton } from '../';
describe( 'PostPreviewButton', () => {
describe( 'setPreviewWindowLink()', () => {
it( 'should do nothing if there is no preview window', () => {
const url = 'https://wordpress.org';
const setter = jest.fn();
const wrapper = shallow( <PostPreviewButton /> );
wrapper.instance().setPreviewWindowLink( url );
expect( setter ).not.toHaveBeenCalled();
} );
it( 'set preview window location to url', () => {
const url = 'https://wordpress.org';
const setter = jest.fn();
const wrapper = shallow( <PostPreviewButton /> );
wrapper.instance().previewWindow = {
get location() {
return {
href: 'about:blank',
};
},
set location( value ) {
setter( value );
},
};
wrapper.instance().setPreviewWindowLink( url );
expect( setter ).toHaveBeenCalledWith( url );
} );
} );
describe( 'getWindowTarget()', () => {
it( 'returns a string unique to the post id', () => {
const instance = new PostPreviewButton( {
postId: 1,
} );
expect( instance.getWindowTarget() ).toBe( 'wp-preview-1' );
} );
} );
describe( 'componentDidUpdate()', () => {
it( 'should change popup location if preview link is available', () => {
const wrapper = shallow(
<PostPreviewButton
postId={ 1 }
currentPostLink="https://wordpress.org/?p=1"
isSaveable
modified="2017-08-03T15:05:50"
/>
);
const previewWindow = { location: {} };
wrapper.instance().previewWindow = previewWindow;
wrapper.setProps( { previewLink: 'https://wordpress.org/?p=1' } );
expect( previewWindow.location ).toBe(
'https://wordpress.org/?p=1'
);
} );
} );
describe( 'openPreviewWindow()', () => {
let windowOpen;
beforeEach( () => {
windowOpen = window.open;
} );
afterEach( () => {
window.open = windowOpen;
} );
it( 'behaves like a regular link if not autosaveable', () => {
const preventDefault = jest.fn();
const autosave = jest.fn();
const setLocation = jest.fn();
window.open = jest.fn( () => ( {
focus: jest.fn(),
set location( url ) {
setLocation( url );
},
} ) );
const wrapper = shallow(
<PostPreviewButton postId={ 1 } autosave={ autosave } />
);
wrapper.simulate( 'click', {
preventDefault,
target: { href: 'https://wordpress.org/?p=1' },
} );
expect( preventDefault ).toHaveBeenCalled();
expect( window.open ).toHaveBeenCalledWith( '', 'wp-preview-1' );
expect( wrapper.instance().previewWindow.focus ).toHaveBeenCalled();
expect( autosave ).not.toHaveBeenCalled();
expect( setLocation ).toHaveBeenCalledWith(
'https://wordpress.org/?p=1'
);
} );
it( 'autosaves the post if autosaveable', () => {
const preventDefault = jest.fn();
const autosave = jest.fn();
window.open = jest.fn( () => ( {
focus: jest.fn(),
document: {
write: jest.fn(),
close: jest.fn(),
},
} ) );
const wrapper = shallow(
<PostPreviewButton
postId={ 1 }
autosave={ autosave }
isAutosaveable
/>
);
wrapper.simulate( 'click', { preventDefault } );
expect( preventDefault ).toHaveBeenCalled();
expect( window.open ).toHaveBeenCalledWith( '', 'wp-preview-1' );
expect( wrapper.instance().previewWindow.focus ).toHaveBeenCalled();
expect( autosave ).toHaveBeenCalled();
expect(
wrapper.instance().previewWindow.document.write.mock
.calls[ 0 ][ 0 ]
).toContain( 'Generating preview…' );
expect(
wrapper.instance().previewWindow.document.close
).toHaveBeenCalled();
} );
} );
describe( 'render()', () => {
it( 'should render previewLink if provided', () => {
const wrapper = shallow(
<PostPreviewButton
postId={ 1 }
isSaveable
previewLink="https://wordpress.org/?p=1&preview=true"
currentPostLink="https://wordpress.org/?p=1"
/>
);
expect( wrapper ).toMatchSnapshot();
} );
it( 'should render currentPostLink otherwise', () => {
const wrapper = shallow(
<PostPreviewButton
postId={ 1 }
isSaveable
currentPostLink="https://wordpress.org/?p=1"
/>
);
expect( wrapper ).toMatchSnapshot();
} );
it( 'should be disabled if post is not saveable', () => {
const wrapper = shallow(
<PostPreviewButton
postId={ 1 }
currentPostLink="https://wordpress.org/?p=1"
/>
);
expect( wrapper.prop( 'disabled' ) ).toBe( true );
} );
} );
} );