Skip to content

Commit

Permalink
[#25] Fix window location href mock
Browse files Browse the repository at this point in the history
  • Loading branch information
liamstevens111 committed Mar 20, 2023
1 parent 949171a commit 8f109f6
Showing 1 changed file with 61 additions and 4 deletions.
65 changes: 61 additions & 4 deletions src/lib/requestManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios';
// import nock from 'nock';

import requestManager, {
createRequestSuccessInterceptor,
Expand Down Expand Up @@ -29,8 +30,19 @@ const mockUserProfileData = {
avatar_url: 'https://secure.gravatar.com/avatar/6733d09432e89459dba795de8312ac2d',
};

// const commonLoginResponse = {
// data: {
// id: '18339',
// type: 'token',
// attributes: {
// ...mockTokenData,
// },
// },
// };

/* eslint-enable camelcase */

/* eslint-disable camelcase */
describe('requestManager', () => {
it('fetches successfully data from an API', async () => {
const responseData = {
Expand Down Expand Up @@ -96,15 +108,20 @@ describe('createResponseErrorInterceptor', () => {
beforeEach(() => {
clearItem('UserProfile');

delete global.window.location;
window.location = {};
global.window = Object.create(window);

Object.defineProperty(window, 'location', {
value: {
href: windowHref,
href: jest.fn(),
},
writable: true,
});
});

afterEach(() => {
window.location.href = windowHref;
});

it('given NON existing tokens and 401 error, redirects to login page', async () => {
const errorResponse = {
name: '',
Expand All @@ -126,9 +143,49 @@ describe('createResponseErrorInterceptor', () => {
const interceptor = createResponseErrorInterceptor();
await interceptor(errorResponse);
} catch (err) {
console.log(err);
expect(err).toBe(errorResponse);
expect(window.location.href).toEqual(LOGIN_URL);
}
});

// it('given existing refresh token and 401 error, updates access token', async () => {
// setItem('UserProfile', { auth: mockTokenData, user: mockUserProfileData });
// // const requestSpy = jest.spyOn(axios, 'request').mockImplementation(() => Promise.resolve(responseData));

// const errorResponse = {
// name: '',
// message: '',
// isAxiosError: true,
// toJSON: () => ({}),
// config: {},
// code: '',
// response: {
// data: {},
// status: 401,
// statusText: '',
// headers: {},
// config: {},
// },
// };

// nock(`${process.env.REACT_APP_API_ENDPOINT}`)
// .defaultReplyHeaders({
// 'access-control-allow-origin': '*',
// 'access-control-allow-credentials': 'true',
// 'access-control-allow-headers': 'Authorization',
// })
// .options('/me')
// .reply(204)
// .get('/me')
// .reply(200, { ...commonLoginResponse, attributes: { ...commonLoginResponse.data.attributes, access_token: 'another' } });

// try {
// const interceptor = createResponseErrorInterceptor();
// await interceptor(errorResponse);
// } catch (err) {
// expect(err).toBe(errorResponse);

// await expect(localStorage.setItem).toHaveBeenLastCalledWith('UserProfile', JSON.stringify({}));
// }
// });
});

0 comments on commit 8f109f6

Please sign in to comment.