forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppBootstrap.test.tsx
More file actions
195 lines (170 loc) · 5.73 KB
/
AppBootstrap.test.tsx
File metadata and controls
195 lines (170 loc) · 5.73 KB
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
194
195
import React, { useContext } from 'react';
import { act, render, screen } from '@testing-library/react';
import { AUTH_HANDLER_TYPE_ANONYMOUS } from '@deephaven/auth-plugins';
import { ApiContext } from '@deephaven/jsapi-bootstrap';
import { type PluginModuleMap, PluginsContext } from '@deephaven/plugin';
import { BROADCAST_LOGIN_MESSAGE } from '@deephaven/jsapi-utils';
import type {
CoreClient,
IdeConnection,
dh as DhType,
} from '@deephaven/jsapi-types';
import { TestUtils } from '@deephaven/test-utils';
import AppBootstrap from './AppBootstrap';
const { asMock } = TestUtils;
jest.mock('react', () => {
const actual = jest.requireActual('react');
return {
...actual,
useContext: jest.fn(actual.useContext),
};
});
const API_URL = 'http://mockserver.net:8111';
const PLUGINS_URL = 'http://mockserver.net:8111/plugins';
const mockGetServerConfigValues = jest.fn(() => Promise.resolve([]));
const mockPluginsPromise = Promise.resolve([]);
jest.mock('../plugins', () => ({
...jest.requireActual('../plugins'),
loadModulePlugins: jest.fn(() => mockPluginsPromise),
}));
const mockChannel = {
postMessage: jest.fn(),
};
jest.mock('@deephaven/jsapi-components', () => ({
...jest.requireActual('@deephaven/jsapi-components'),
useBroadcastChannel: jest.fn(() => mockChannel),
useBroadcastLoginListener: jest.fn(),
}));
const mockChildText = 'Mock Child';
const mockChild = <div>{mockChildText}</div>;
const mockPluginModuleMapEmpty: PluginModuleMap = new Map();
function expectMockChild() {
return expect(screen.queryByText(mockChildText));
}
function renderComponent(client: CoreClient) {
const api = TestUtils.createMockProxy<DhType>({
CoreClient: jest
.fn()
.mockImplementation(() => client) as unknown as CoreClient,
});
return render(
<ApiContext.Provider value={api}>
<AppBootstrap serverUrl={API_URL} pluginsUrl={PLUGINS_URL}>
{mockChild}
</AppBootstrap>
</ApiContext.Provider>
);
}
beforeEach(() => {
jest.clearAllMocks();
});
it('should throw if api has not been bootstrapped', () => {
TestUtils.disableConsoleOutput();
asMock(useContext).mockImplementation(context => {
// ThemeBootstrap doesn't render children until plugins are loaded. We need
// a non-null PluginsContext value to render the children and get the expected
// missing api error.
if (context === PluginsContext) {
return mockPluginModuleMapEmpty;
}
return jest.requireActual('react').useContext(context);
});
expect(() =>
render(
<AppBootstrap serverUrl={API_URL} pluginsUrl={PLUGINS_URL}>
{mockChild}
</AppBootstrap>
)
).toThrow();
expectMockChild().toBeNull();
});
it('should display an error if no login plugin matches the provided auth handlers', async () => {
const authConfigValues: [string, string][] = [
['AuthHandlers', `MockAuthHandler`],
];
const mockGetAuthConfigValues = jest.fn(() =>
Promise.resolve(authConfigValues)
);
const mockLogin = jest.fn(() => Promise.resolve());
const client = TestUtils.createMockProxy<CoreClient>({
getAuthConfigValues: mockGetAuthConfigValues,
getServerConfigValues: mockGetServerConfigValues,
login: mockLogin,
});
renderComponent(client);
expectMockChild().toBeNull();
expect(mockGetAuthConfigValues).toHaveBeenCalled();
await act(async () => {
await mockPluginsPromise;
});
expectMockChild().toBeNull();
expect(mockLogin).not.toHaveBeenCalled();
expect(
screen.queryByText(
'No login plugins found, please register a login plugin for auth handlers: MockAuthHandler'
)
).not.toBeNull();
});
it('should log in automatically when the anonymous handler is supported', async () => {
const authConfigValues: [string, string][] = [
['AuthHandlers', `${AUTH_HANDLER_TYPE_ANONYMOUS}`],
];
const mockGetAuthConfigValues = jest.fn(() =>
Promise.resolve(authConfigValues)
);
let mockLoginResolve;
const mockLogin = jest.fn(
() =>
new Promise<void>(resolve => {
mockLoginResolve = resolve;
})
);
let mockConnectionResolve;
const mockGetAsConnection = jest.fn(
() =>
new Promise<IdeConnection>(resolve => {
mockConnectionResolve = resolve;
})
);
const mockConnection = TestUtils.createMockProxy<IdeConnection>({});
const client = TestUtils.createMockProxy<CoreClient>({
getAsIdeConnection: mockGetAsConnection,
getAuthConfigValues: mockGetAuthConfigValues,
getServerConfigValues: mockGetServerConfigValues,
login: mockLogin,
});
renderComponent(client);
expectMockChild().toBeNull();
expect(mockLogin).not.toHaveBeenCalled();
// Wait for plugins to load
await act(async () => {
await mockPluginsPromise;
});
expect(mockChannel.postMessage).not.toHaveBeenCalled();
expectMockChild().toBeNull();
expect(mockLogin).toHaveBeenCalled();
expect(screen.queryByTestId('auth-base-loading')).not.toBeNull();
expect(screen.queryByTestId('auth-base-loading-spinner')).not.toBeNull();
// Wait for login to complete
await act(async () => {
mockLoginResolve();
});
expect(screen.queryByTestId('auth-base-loading')).toBeNull();
expect(screen.queryByTestId('connection-bootstrap-loading')).not.toBeNull();
expect(
screen.queryByTestId('connection-bootstrap-loading-spinner')
).not.toBeNull();
expect(screen.queryByText(mockChildText)).toBeNull();
expect(mockChannel.postMessage).toHaveBeenCalledWith(
expect.objectContaining({
message: BROADCAST_LOGIN_MESSAGE,
})
);
// Wait for IdeConnection to resolve
await act(async () => {
mockConnectionResolve(mockConnection);
});
expect(screen.queryByTestId('auth-base-loading')).toBeNull();
expect(screen.queryByTestId('connection-bootstrap-loading')).toBeNull();
expectMockChild().not.toBeNull();
});