7383-testToMaster #370
|
@ -157,5 +157,8 @@ export function useSession() {
|
|||
destroy,
|
||||
login,
|
||||
isLoggedIn,
|
||||
checkValidity,
|
||||
setSession,
|
||||
renewToken,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -23,5 +23,6 @@ export function useTokenConfig() {
|
|||
|
||||
return {
|
||||
fetch,
|
||||
state,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { vi, describe, expect, it } from 'vitest';
|
||||
import { axios } from 'app/test/vitest/helper';
|
||||
import { vi, describe, expect, it, beforeAll, beforeEach } from 'vitest';
|
||||
import { axios, flushPromises } from 'app/test/vitest/helper';
|
||||
import { useSession } from 'composables/useSession';
|
||||
import { useState } from 'composables/useState';
|
||||
|
||||
|
@ -63,7 +63,9 @@ describe('session', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('login', () => {
|
||||
describe(
|
||||
'login',
|
||||
() => {
|
||||
const expectedUser = {
|
||||
id: 999,
|
||||
name: `T'Challa`,
|
||||
|
@ -139,5 +141,70 @@ describe('session', () => {
|
|||
|
||||
await session.destroy(); // this clears token and user for any other test
|
||||
});
|
||||
},
|
||||
{}
|
||||
);
|
||||
|
||||
describe('RenewToken', () => {
|
||||
const expectedToken = 'myToken';
|
||||
const expectedTokenMultimedia = 'myTokenMultimedia';
|
||||
const currentDate = new Date();
|
||||
beforeAll(() => {
|
||||
const tokenConfig = {
|
||||
id: 1,
|
||||
renewPeriod: 21600,
|
||||
courtesyTime: 60,
|
||||
renewInterval: 300,
|
||||
};
|
||||
state.setTokenConfig(tokenConfig);
|
||||
sessionStorage.setItem('renewPeriod', 1);
|
||||
});
|
||||
it('NOT Should renewToken', async () => {
|
||||
const data = {
|
||||
token: expectedToken,
|
||||
tokenMultimedia: expectedTokenMultimedia,
|
||||
keepLogin: false,
|
||||
ttl: 1,
|
||||
created: Date.now(),
|
||||
};
|
||||
session.setSession(data);
|
||||
expect(sessionStorage.getItem('keepLogin')).toBeFalsy();
|
||||
expect(sessionStorage.getItem('created')).toBeDefined();
|
||||
expect(sessionStorage.getItem('ttl')).toEqual(1);
|
||||
await session.checkValidity();
|
||||
expect(sessionStorage.getItem('token')).toEqual(expectedToken);
|
||||
expect(sessionStorage.getItem('tokenMultimedia')).toEqual(
|
||||
expectedTokenMultimedia
|
||||
);
|
||||
});
|
||||
it('Should renewToken', async () => {
|
||||
currentDate.setMinutes(currentDate.getMinutes() - 100);
|
||||
const data = {
|
||||
token: expectedToken,
|
||||
tokenMultimedia: expectedTokenMultimedia,
|
||||
keepLogin: false,
|
||||
ttl: 1,
|
||||
created: currentDate,
|
||||
};
|
||||
session.setSession(data);
|
||||
|
||||
vi.spyOn(axios, 'post')
|
||||
.mockResolvedValueOnce({
|
||||
data: { id: '' },
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
data: {
|
||||
id: '',
|
||||
},
|
||||
});
|
||||
expect(sessionStorage.getItem('keepLogin')).toBeFalsy();
|
||||
expect(sessionStorage.getItem('created')).toBeDefined();
|
||||
expect(sessionStorage.getItem('ttl')).toEqual(1);
|
||||
await session.checkValidity();
|
||||
expect(sessionStorage.getItem('token')).not.toEqual(expectedToken);
|
||||
expect(sessionStorage.getItem('tokenMultimedia')).not.toEqual(
|
||||
expectedTokenMultimedia
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
import { vi, describe, expect, it } from 'vitest';
|
||||
import { axios, flushPromises } from 'app/test/vitest/helper';
|
||||
import { useTokenConfig } from 'composables/useTokenConfig';
|
||||
const tokenConfig = useTokenConfig();
|
||||
|
||||
describe('useTokenConfig', () => {
|
||||
describe('fetch', () => {
|
||||
it('should call setTokenConfig of the state with the expected data', async () => {
|
||||
const data = {
|
||||
id: 1,
|
||||
renewPeriod: 21600,
|
||||
courtesyTime: 60,
|
||||
renewInterval: 300,
|
||||
};
|
||||
vi.spyOn(axios, 'get').mockResolvedValueOnce({
|
||||
data,
|
||||
});
|
||||
|
||||
vi.spyOn(tokenConfig.state, 'setTokenConfig');
|
||||
|
||||
tokenConfig.fetch();
|
||||
|
||||
await flushPromises();
|
||||
|
||||
expect(tokenConfig.state.setTokenConfig).toHaveBeenCalledWith(data);
|
||||
|
||||
const renewPeriod = sessionStorage.getItem('renewPeriod');
|
||||
expect(renewPeriod).toEqual(data.renewPeriod);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue