Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkoKukovec committed Apr 16, 2024
1 parent 50f1570 commit ea17250
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/utils/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ describe('utils/register', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.navigator.serviceWorker = {
register: jest.fn(() => Promise.resolve()),
register: jest.fn(() =>
Promise.resolve({
unregister: jest.fn(),
})
),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
};
Expand Down
7 changes: 6 additions & 1 deletion src/worker/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ describe('worker/fetch', () => {
});

beforeEach(() => {
jest.spyOn(globalThis, 'fetch');
Object.defineProperty(globalThis, 'fetch', {
configurable: true,
enumerable: true,
value: jest.fn(),
writable: true,
});
});

describe('refreshToken', () => {
Expand Down
18 changes: 13 additions & 5 deletions src/worker/interceptor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ describe('worker/interceptor', () => {
});

beforeEach(() => {
jest.spyOn(globalThis, 'fetch');
Object.defineProperty(globalThis, 'fetch', {
configurable: true,
enumerable: true,
value: jest.fn(),
writable: true,
});
(getAuthState as jest.Mock).mockResolvedValue({
allowList: undefined,
});
Expand Down Expand Up @@ -113,10 +118,12 @@ describe('worker/interceptor', () => {

const location = response.headers.get('location');

expect(location.startsWith('https://example.com/login?client_id=fooClientId&response_type=token&state='))
.toBe(true);
expect(location.endsWith('&scope=&redirect_uri=https%3A%2F%2Fexample.com%2Ffoobar%2Fcallback%2Ffoo'))
.toBe(true);
// eslint-disable-next-line max-len
expect(location.startsWith('https://example.com/login?client_id=fooClientId&response_type=token&state=')).toBe(
true
);
// eslint-disable-next-line max-len
expect(location.endsWith('&scope=&redirect_uri=https%3A%2F%2Fexample.com%2Ffoobar%2Fcallback%2Ffoo')).toBe(true);
expect(response.status).toBe(302);
});

Expand Down Expand Up @@ -157,6 +164,7 @@ describe('worker/interceptor', () => {
'foobartest123',
'foo',
expect.stringMatching(/[a-z0-9]{16}/),
'https://example.com',
expect.stringMatching(/[a-z0-9]{128}/)
);
});
Expand Down
15 changes: 10 additions & 5 deletions src/worker/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ describe('worker/operations', () => {
});

beforeEach(() => {
jest.spyOn(globalThis, 'fetch');
Object.defineProperty(globalThis, 'fetch', {
configurable: true,
enumerable: true,
value: jest.fn(),
writable: true,
});
(jwtDecode as jest.Mock).mockImplementation((data) => data);
});

Expand All @@ -26,8 +31,8 @@ describe('worker/operations', () => {

(getAuthState as jest.Mock).mockReturnValue(state);

await expect(createSession('', 'mockProvider', '', 'http://example.com'))
.rejects.toThrow('No config found');
// eslint-disable-next-line max-len
await expect(createSession('', 'mockProvider', '', 'http://example.com')).rejects.toThrow('No config found');
});

it('should fail if there is no valid providers', async () => {
Expand Down Expand Up @@ -55,8 +60,8 @@ describe('worker/operations', () => {

(getAuthState as jest.Mock).mockReturnValue(state);

await expect(createSession('', 'mockProvider', '123', 'http://example.com'))
.rejects.toThrow('Invalid state');
// eslint-disable-next-line max-len
await expect(createSession('', 'mockProvider', '123', 'http://example.com')).rejects.toThrow('Invalid state');
});

it('should work for token flow', async () => {
Expand Down

0 comments on commit ea17250

Please sign in to comment.