Files
directus/src/api.test.ts
dependabot-preview[bot] 26960961e8 Bump prettier from 1.19.1 to 2.0.2 (#239)
* Bump prettier from 1.19.1 to 2.0.2

Bumps [prettier](https://github.com/prettier/prettier) from 1.19.1 to 2.0.2.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/master/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/1.19.1...2.0.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Add dangling comma's

* Update eslint config to match prettier default

* Make eslint match prettier for real this time

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
2020-03-24 10:51:30 -04:00

172 lines
3.4 KiB
TypeScript

import Vue from 'vue';
import VueCompositionAPI from '@vue/composition-api';
import { onRequest, onResponse, onError, getRootPath, Error } from './api';
import * as auth from '@/auth';
import { useRequestsStore } from '@/stores/requests';
const defaultError: Error = {
config: {},
isAxiosError: false,
toJSON: () => ({}),
name: 'error',
message: '',
response: {
data: null,
status: 200,
statusText: 'OK',
headers: {},
config: {
id: 'abc',
},
},
};
describe('API', () => {
beforeEach(() => {
jest.spyOn(auth, 'logout');
jest.spyOn(auth, 'checkAuth');
Vue.use(VueCompositionAPI);
window = Object.create(window);
});
it('Calculates the correct API root URL based on window', () => {
Object.defineProperty(window, 'location', {
value: {
pathname: '/api/nested/admin',
},
writable: true,
});
const result = getRootPath();
expect(result).toBe('/api/nested/');
});
it('Calls startRequest on the store on any request', () => {
const store = useRequestsStore({});
const spy = jest.spyOn(store, 'startRequest');
spy.mockImplementation(() => 'abc');
const newRequest = onRequest({});
expect(spy).toHaveBeenCalled();
expect(newRequest.id).toBe('abc');
});
it('Calls endRequest on responses', () => {
const store = useRequestsStore({});
const spy = jest.spyOn(store, 'endRequest');
onResponse({
data: null,
status: 200,
statusText: 'OK',
headers: {},
config: {
id: 'abc',
},
});
expect(spy).toHaveBeenCalledWith('abc');
});
it('Calls endRequest on errors', async () => {
const store = useRequestsStore({});
const spy = jest.spyOn(store, 'endRequest');
try {
await onError({
...defaultError,
});
} catch {}
expect(spy).toHaveBeenCalledWith('abc');
});
it('Passes the error on to the next catch handler on unrelated 401 errors', async () => {
const error = {
...defaultError,
response: {
...defaultError.response,
status: 401,
config: {
id: 'abc',
},
data: {
error: {
code: -5,
},
},
},
};
expect(onError(error)).rejects.toEqual(error);
});
it('Checks the auth status on 401+3 errors', async () => {
try {
await onError({
...defaultError,
response: {
...defaultError.response,
config: {
id: 'abc',
},
status: 401,
data: {
error: {
code: 3,
},
},
},
});
} catch {
expect(auth.checkAuth).toHaveBeenCalled();
}
});
it('Forces a logout when the users is not logged in on 401+3 errors', async () => {
(auth.checkAuth as jest.Mock).mockImplementation(async () => false);
try {
await onError({
...defaultError,
response: {
...defaultError.response,
config: {
id: 'abc',
},
status: 401,
data: {
error: {
code: 3,
},
},
},
});
} catch {
expect(auth.logout).toHaveBeenCalledWith({
reason: auth.LogoutReason.ERROR_SESSION_EXPIRED,
});
}
});
it('Does not call logout if the user is logged in on 401+3 error', async () => {
(auth.checkAuth as jest.Mock).mockImplementation(async () => true);
try {
await onError({
...defaultError,
response: {
...defaultError.response,
config: {
id: 'abc',
},
status: 401,
data: {
error: {
code: 3,
},
},
},
});
} catch {
expect(auth.logout).not.toHaveBeenCalled();
}
});
});