-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathGithubAuthButton.test.jsx
193 lines (154 loc) · 5.87 KB
/
GithubAuthButton.test.jsx
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
189
190
191
192
193
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { spy, stub, restore } from 'sinon';
import '@testing-library/jest-dom';
import globals from '../globals';
import api from '@util/federalistApi';
import GithubAuthButton from './GithubAuthButton';
const BUTTON_TEXT = `Connect with GitHub`;
const text = 'Hello World';
// We need to create the MessageEvent to set the origin
// and data when testing
// https://github.com/jsdom/jsdom/issues/2745#issuecomment-1207414024
function createPostMessageEvent(data, origin) {
return new MessageEvent('message', {
source: window,
origin,
data,
});
}
function createStubs() {
const onFailure = spy();
const onSuccess = spy();
const apiStub = stub(api, 'revokeApplicationGrant').resolves();
const focusStub = stub().resolves();
const closeStub = stub().returns();
const windowStub = stub(window, 'open').returns({
focus: focusStub,
close: closeStub,
});
return {
onFailure,
onSuccess,
apiStub,
focusStub,
closeStub,
windowStub,
};
}
describe('<GithubAuthButton/>', () => {
let onFailure;
let onSuccess;
let apiStub;
let focusStub;
let closeStub;
let windowStub;
const user = userEvent.setup();
beforeAll(() => {
// Set window origin based on globals APP_HOSTNAME
window['origin'] = globals.APP_HOSTNAME;
// Set screen height and width
window['screen'] = {
width: 1200,
height: 1000,
};
});
beforeEach(() => {
({ onFailure, onSuccess, apiStub, focusStub, closeStub, windowStub } = createStubs());
});
afterEach(() => restore());
it('renders', () => {
const props = { onFailure, onSuccess, text };
render(<GithubAuthButton {...props} />);
expect(screen.getByText(text)).toBeTruthy();
const button = screen.getByRole('button');
expect(button).toHaveTextContent(BUTTON_TEXT);
expect(onFailure.notCalled).toBe(true);
expect(onSuccess.notCalled).toBe(true);
});
it('opens window, calls the revokeFirst and then succeeds', async () => {
const props = { onFailure, onSuccess, text, revokeFirst: true };
render(<GithubAuthButton {...props} />);
await user.click(screen.getByRole('button'));
expect(apiStub.calledOnce).toBe(true);
await waitFor(() => expect(windowStub.calledOnce).toBe(true));
await waitFor(() => expect(focusStub.calledOnce).toBe(true));
await waitFor(() =>
window.dispatchEvent(createPostMessageEvent('success', window.origin)),
);
expect(closeStub.calledOnce).toBe(true);
expect(onSuccess.calledOnce).toBe(true);
expect(onFailure.notCalled).toBe(true);
});
it('opens window with revokeFirst and fails authorize', async () => {
const props = { onFailure, onSuccess, text, revokeFirst: true };
render(<GithubAuthButton {...props} />);
await user.click(screen.getByRole('button'));
expect(apiStub.calledOnce).toBe(true);
await waitFor(() => expect(windowStub.calledOnce).toBe(true));
await waitFor(() => expect(focusStub.calledOnce).toBe(true));
await waitFor(() =>
window.dispatchEvent(createPostMessageEvent('fail', window.origin)),
);
expect(closeStub.notCalled).toBe(true);
expect(onSuccess.notCalled).toBe(true);
expect(onFailure.calledOnce).toBe(true);
});
it('opens window with revokeFirst and fails origin', async () => {
const props = { onFailure, onSuccess, text, revokeFirst: true };
render(<GithubAuthButton {...props} />);
await user.click(screen.getByRole('button'));
expect(apiStub.calledOnce).toBe(true);
await waitFor(() => expect(windowStub.calledOnce).toBe(true));
await waitFor(() => expect(focusStub.calledOnce).toBe(true));
await waitFor(() =>
window.dispatchEvent(createPostMessageEvent('success', 'http://wrongorigin')),
);
expect(closeStub.notCalled).toBe(true);
expect(onSuccess.notCalled).toBe(true);
expect(onFailure.calledOnce).toBe(true);
});
it('opens window without revokeFirst and then succeeds', async () => {
const props = { onFailure, onSuccess, text };
render(<GithubAuthButton {...props} />);
await user.click(screen.getByRole('button'));
expect(apiStub.notCalled).toBe(true);
await waitFor(() => expect(windowStub.calledOnce).toBe(true));
await waitFor(() => expect(focusStub.calledOnce).toBe(true));
await waitFor(() =>
window.dispatchEvent(createPostMessageEvent('success', window.origin)),
);
expect(closeStub.calledOnce).toBe(true);
expect(onSuccess.calledOnce).toBe(true);
expect(onFailure.notCalled).toBe(true);
});
it('opens window without revokeFirst and fails authorize', async () => {
const props = { onFailure, onSuccess, text };
render(<GithubAuthButton {...props} />);
await user.click(screen.getByRole('button'));
expect(apiStub.notCalled).toBe(true);
await waitFor(() => expect(windowStub.calledOnce).toBe(true));
await waitFor(() => expect(focusStub.calledOnce).toBe(true));
await waitFor(() =>
window.dispatchEvent(createPostMessageEvent('fail', window.origin)),
);
expect(closeStub.notCalled).toBe(true);
expect(onSuccess.notCalled).toBe(true);
expect(onFailure.calledOnce).toBe(true);
});
it('opens window without revokeFirst and fails origin', async () => {
const props = { onFailure, onSuccess, text };
render(<GithubAuthButton {...props} />);
await user.click(screen.getByRole('button'));
expect(apiStub.notCalled).toBe(true);
await waitFor(() => expect(windowStub.calledOnce).toBe(true));
await waitFor(() => expect(focusStub.calledOnce).toBe(true));
await waitFor(() =>
window.dispatchEvent(createPostMessageEvent('success', 'http://wrongorigin')),
);
expect(closeStub.notCalled).toBe(true);
expect(onSuccess.notCalled).toBe(true);
expect(onFailure.calledOnce).toBe(true);
});
});