Skip to content

Commit

Permalink
Fix front-end testing (oops), and fix user-store probably being mangl…
Browse files Browse the repository at this point in the history
…ed by weird merging.
  • Loading branch information
MelissaAutumn committed Jan 15, 2024
1 parent e15654b commit aea9815
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 44 deletions.
4 changes: 2 additions & 2 deletions frontend/src/stores/user-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const initialUserObject = {

export const useUserStore = defineStore('user', {
state: () => ({
data: useLocalStorage('tba/user', initialUserObject),
data: useLocalStorage('tba/user', structuredClone(initialUserObject)),
}),
actions: {
exists() {
Expand Down Expand Up @@ -43,7 +43,7 @@ export const useUserStore = defineStore('user', {
}
});

return await this.signedUrl(fetch);
return await this.updateSignedUrl(fetch);
},
// retrieve the current signed url and update store
async updateSignedUrl(fetch) {
Expand Down
18 changes: 9 additions & 9 deletions frontend/test/stores/alert-store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,34 @@ describe('Site Notifications Store', () => {
test('visible', () => {
const alert = useSiteNotificationStore();
alert.show(123, 'title', 'message', 'url');
expect(alert.isVisible === true);
expect(alert.isVisible).toBe(true);
});
test('attributes', () => {
const alert = useSiteNotificationStore();
alert.show(123, 'title', 'message', 'url');
expect(alert.title === 'title');
expect(alert.actionUrl === 'url');
expect(alert.message === 'message');
expect(alert.title).toBe('title');
expect(alert.actionUrl).toBe('url');
expect(alert.message).toBe('message');
});
test('same', () => {
const alert = useSiteNotificationStore();
alert.show(123, 'title', 'message', 'url');
expect(alert.isSameNotification(123) === true);
expect(alert.isSameNotification(123)).toBe(true);
});
test('invisible at start', () => {
const alert = useSiteNotificationStore();
expect(alert.isVisible === false);
expect(alert.isVisible).toBe(false);
});
test('invisible after reset', () => {
const alert = useSiteNotificationStore();
alert.show(123, 'title', 'message', 'url');
alert.reset();
expect(alert.isVisible === false);
expect(alert.isVisible).toBe(false);
});
test('lock', () => {
const alert = useSiteNotificationStore();
alert.lock(123);
expect(alert.isVisible === false);
expect(alert.isSameNotification(123) === true);
expect(alert.isVisible).toBe(false);
expect(alert.isSameNotification(123)).toBe(true);
});
});
18 changes: 9 additions & 9 deletions frontend/test/stores/appointment-store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,36 @@ describe('Appointment Store', () => {

test('init', () => {
const apmt = useAppointmentStore();
expect(apmt.isLoaded === false);
expect(apmt.appointments.length === 0);
expect(apmt.isLoaded).toBe(false);
expect(apmt.appointments.length).toBe(0);
});

test('fetch', async () => {
const apmt = useAppointmentStore();
await apmt.fetch(createFetch({ baseUrl: API_URL }));
expect(apmt.appointments.length === 2);
expect(apmt.appointments[0].slots.length === 3);
expect(apmt.appointments.length).toBe(2);
expect(apmt.appointments[0].slots.length).toBe(3);
});

test('pending', async () => {
const apmt = useAppointmentStore();
await apmt.fetch(createFetch({ baseUrl: API_URL }));
expect(apmt.pendingAppointments.length === 1);
expect(apmt.pendingAppointments.length).toBe(1);
});

test('reset', async () => {
const apmt = useAppointmentStore();
await apmt.fetch(createFetch({ baseUrl: API_URL }));

// Check if appointments exist
expect(apmt.isLoaded === true);
expect(apmt.appointments.length === 2);
expect(apmt.isLoaded).toBe(true);
expect(apmt.appointments.length).toBe(2);

// Reset the user which should null all user data.
apmt.reset();

// Ensure our data is null/don't exist
expect(apmt.isLoaded === false);
expect(apmt.appointments.length === 0);
expect(apmt.isLoaded).toBe(false);
expect(apmt.appointments.length).toBe(0);
});
});
18 changes: 9 additions & 9 deletions frontend/test/stores/calendar-store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,41 +56,41 @@ describe('Calendar Store', () => {

test('init', () => {
const calStore = useCalendarStore();
expect(calStore.isLoaded === false);
expect(calStore.allCalendars.length === 0);
expect(calStore.isLoaded).toBe(false);
expect(calStore.allCalendars.length).toBe(0);
});

test('fetch', async () => {
const calStore = useCalendarStore();
await calStore.fetch(createFetch({ baseUrl: API_URL }));
expect(calStore.allCalendars.length === 2);
expect(calStore.allCalendars.length).toBe(2);
});

test('unconnected', async () => {
const calStore = useCalendarStore();
await calStore.fetch(createFetch({ baseUrl: API_URL }));
expect(calStore.unconnectedCalendars.length === 1);
expect(calStore.unconnectedCalendars.length).toBe(1);
});

test('connected', async () => {
const calStore = useCalendarStore();
await calStore.fetch(createFetch({ baseUrl: API_URL }));
expect(calStore.connectedCalendars.length === 1);
expect(calStore.connectedCalendars.length).toBe(1);
});

test('reset', async () => {
const calStore = useCalendarStore();
await calStore.fetch(createFetch({ baseUrl: API_URL }));

// Check if calendars exist
expect(calStore.isLoaded === true);
expect(calStore.allCalendars.length === 2);
expect(calStore.isLoaded).toBe(true);
expect(calStore.allCalendars.length).toBe(2);

// Reset the user which should null all user data.
calStore.reset();

// Ensure our data is null/don't exist
expect(calStore.isLoaded === false);
expect(calStore.allCalendars.length === 0);
expect(calStore.isLoaded).toBe(false);
expect(calStore.allCalendars.length).toBe(0);
});
});
58 changes: 43 additions & 15 deletions frontend/test/stores/user-store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ const API_URL = 'http://localhost';
const TEST_USERNAME = 'test';
const TEST_PASSWORD = 'password';

const beforeFetch = async (options, user) => {
if (user.exists()) {
const token = await user.data.accessToken;
options.headers.Authorization = `Bearer ${token}`;
}
return { options };
};

const restHandlers = [
http.post(`${API_URL}/token`, async ({ request }) => {
const formData = await request.formData();
Expand All @@ -29,7 +37,7 @@ const restHandlers = [
}

return HttpResponse.json({
accessToken: 'You have access!',
access_token: 'You have access!',
});
}),
http.get(`${API_URL}/me`, async (request) => {
Expand All @@ -51,11 +59,25 @@ const restHandlers = [
avatarUrl: null,
});
}),
http.get(`${API_URL}/me/signature`, async (request) => {
const headers = await request.request.headers;

// Fail without an access token
if (!headers.get('authorization')) {
return HttpResponse.json({
detail: 'Not authenticated',
}, { status: 403 });
}

return HttpResponse.json({
url: 'https://blah',
});
}),
];

const server = setupServer(...restHandlers);
server.events.on('request:start', ({ request }) => {
// console.log('Outgoing:', request.method, request.url);
console.log('Outgoing:', request.method, request.url);
});

describe('User Store', () => {
Expand All @@ -76,11 +98,12 @@ describe('User Store', () => {
test('exists', () => {
const user = useUserStore();
user.data.accessToken = 'abc';
expect(user.exists() === true);
expect(user.exists()).toBe(true);
});
test('does not exist', () => {
const user = useUserStore();
expect(user.exists() === false);
user.reset();
expect(user.exists()).toBe(false);
});
test('reset', () => {
const user = useUserStore();
Expand All @@ -101,9 +124,10 @@ describe('User Store', () => {
user.reset();

// Ensure our data is null/don't exist
expect(user.exists() === false);
expect(user.data.name === null);
expect(user.data.email === null);
expect(user.exists()).toBe(false);
expect(user.data.name).toBeNull();
expect(user.data.email).toBeNull();
expect(user.data.signedUrl).toBeNull();
});
test('login fails', async () => {
const user = useUserStore();
Expand All @@ -119,15 +143,19 @@ describe('User Store', () => {

const response = await user.login(createFetch({
baseUrl: API_URL,
options: {
beforeFetch: async ({ options }) => beforeFetch(options, user),
},
}), TEST_USERNAME, `${TEST_PASSWORD}`);

expect(response === true);
expect(user.exists());
expect(user.data.accessToken);
expect(user.data.username);
expect(user.data.email);
expect(user.data.name);
expect(user.data.level);
expect(user.data.timezone);
expect(response).toBe(true);
expect(user.exists()).toBe(true);
expect(user.data.accessToken).toBeTruthy();
expect(user.data.username).toBeTruthy();
expect(user.data.email).toBeTruthy();
expect(user.data.name).toBeTruthy();
expect(user.data.level).toBeTruthy();
expect(user.data.timezone).toBeTruthy();
expect(user.data.signedUrl).toBeTruthy();
});
});

0 comments on commit aea9815

Please sign in to comment.