Skip to content

Commit 89aeee3

Browse files
committed
test
1 parent b6d6367 commit 89aeee3

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

tests/unit/auth.test.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,31 @@ describe('Auth Module', () => {
77
let scope;
88
const appId = 'test-app-id';
99
const serverUrl = 'https://api.base44.com';
10-
10+
1111
beforeEach(() => {
12+
// Mock window.addEventListener and document for analytics module
13+
if (typeof window !== 'undefined') {
14+
if (!window.addEventListener) {
15+
window.addEventListener = vi.fn();
16+
window.removeEventListener = vi.fn();
17+
}
18+
}
19+
if (typeof document === 'undefined') {
20+
global.document = {
21+
referrer: '',
22+
visibilityState: 'visible'
23+
};
24+
}
25+
1226
// Create a new client for each test
1327
base44 = createClient({
1428
serverUrl,
1529
appId,
1630
});
17-
31+
1832
// Create a nock scope for mocking API calls
1933
scope = nock(serverUrl);
20-
34+
2135
// Enable request debugging for Nock
2236
nock.disableNetConnect();
2337
nock.emitter.on('no match', (req) => {
@@ -316,33 +330,33 @@ describe('Auth Module', () => {
316330
global.window = {
317331
location: mockLocation
318332
};
319-
333+
320334
const redirectUrl = 'https://example.com/logout-success';
321335
base44.auth.logout(redirectUrl);
322-
323-
// Verify redirect
324-
expect(mockLocation.href).toBe(redirectUrl);
325-
336+
337+
// Verify redirect to server-side logout endpoint with from_url parameter
338+
const expectedUrl = `${serverUrl}/api/apps/${appId}/auth/logout?from_url=${encodeURIComponent(redirectUrl)}`;
339+
expect(mockLocation.href).toBe(expectedUrl);
340+
326341
// Restore window
327342
global.window = originalWindow;
328343
});
329344

330-
test('should reload page when no redirect URL is provided', async () => {
331-
// Mock window object with reload function
332-
const mockReload = vi.fn();
345+
test('should redirect to logout endpoint when no redirect URL is provided', async () => {
346+
// Mock window object
347+
const mockLocation = { href: 'https://example.com/current-page' };
333348
const originalWindow = global.window;
334349
global.window = {
335-
location: {
336-
reload: mockReload
337-
}
350+
location: mockLocation
338351
};
339-
352+
340353
// Call logout without redirect URL
341354
base44.auth.logout();
342-
343-
// Verify page reload was called
344-
expect(mockReload).toHaveBeenCalledTimes(1);
345-
355+
356+
// Verify redirect to server-side logout endpoint with current page as from_url
357+
const expectedUrl = `${serverUrl}/api/apps/${appId}/auth/logout?from_url=${encodeURIComponent('https://example.com/current-page')}`;
358+
expect(mockLocation.href).toBe(expectedUrl);
359+
346360
// Restore window
347361
global.window = originalWindow;
348362
});

0 commit comments

Comments
 (0)