setup stores and vitest
This commit is contained in:
105
test/account.store.spec.ts
Normal file
105
test/account.store.spec.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { describe, expect, afterEach, beforeEach, it, vi } from 'vitest';
|
||||
import { useAccountStore } from '../stores/account.store';
|
||||
import { setActivePinia, createPinia } from 'pinia';
|
||||
|
||||
import { FullDBUser } from '~/lib/services/service.types';
|
||||
|
||||
const fakeInitAccountStoreAdmin = (accountStore: any) => {
|
||||
const dbUser: FullDBUser = {
|
||||
id: 1,
|
||||
name: 'John Doe',
|
||||
memberships: [
|
||||
{ account_id: 1, access: 'ADMIN' },
|
||||
{ account_id: 2, access: 'READ_ONLY' }
|
||||
]
|
||||
} as any;
|
||||
accountStore.dbUser = dbUser;
|
||||
accountStore.activeAccountId = 1;
|
||||
};
|
||||
|
||||
describe('Account Store', async () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
});
|
||||
|
||||
it('should initialize the store', async () => {
|
||||
// stub the useNuxtApp function with a mock client
|
||||
vi.stubGlobal('useNuxtApp', () => ({
|
||||
$client: {
|
||||
auth: {
|
||||
getDBUser: {
|
||||
query: () => ({
|
||||
dbUser: {
|
||||
id: 1,
|
||||
name: 'John Doe',
|
||||
memberships: []
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
account: {
|
||||
getActiveAccountId: {
|
||||
query: () => ({ activeAccountId: 1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
const accountStore = useAccountStore();
|
||||
|
||||
// method under test
|
||||
await accountStore.init();
|
||||
|
||||
expect(accountStore.dbUser).toEqual({
|
||||
id: 1,
|
||||
name: 'John Doe',
|
||||
memberships: []
|
||||
});
|
||||
|
||||
expect(accountStore.activeAccountId).toEqual(1);
|
||||
});
|
||||
|
||||
it('should get active account members', async () => {
|
||||
// stub the useNuxtApp function with a mock client
|
||||
vi.stubGlobal('useNuxtApp', () => ({
|
||||
$client: {
|
||||
account: {
|
||||
getAccountMembers: {
|
||||
useQuery: () => ({
|
||||
data: { value: { memberships: [new Object() as any] } }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
const accountStore = useAccountStore();
|
||||
fakeInitAccountStoreAdmin(accountStore);
|
||||
|
||||
// method under test
|
||||
await accountStore.getActiveAccountMembers();
|
||||
|
||||
expect(accountStore.activeAccountMembers.length).toEqual(1);
|
||||
});
|
||||
|
||||
it('should get an active membership', async () => {
|
||||
const accountStore = useAccountStore();
|
||||
fakeInitAccountStoreAdmin(accountStore);
|
||||
|
||||
expect(accountStore.activeMembership).toEqual({
|
||||
account_id: 1,
|
||||
access: 'ADMIN'
|
||||
});
|
||||
});
|
||||
|
||||
it('should signout', async () => {
|
||||
const accountStore = useAccountStore();
|
||||
fakeInitAccountStoreAdmin(accountStore);
|
||||
|
||||
await accountStore.signout();
|
||||
|
||||
expect(accountStore.dbUser).toBeNull();
|
||||
expect(accountStore.activeAccountId).toBeNull();
|
||||
expect(accountStore.activeAccountMembers.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
54
test/notify.store.spec.ts
Normal file
54
test/notify.store.spec.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { describe, test, expect, beforeEach } from 'vitest';
|
||||
import { setup, $fetch } from '@nuxt/test-utils';
|
||||
import { useNotifyStore, NotificationType } from '../stores/notify.store';
|
||||
import { setActivePinia, createPinia } from 'pinia';
|
||||
|
||||
describe('Notify Store', async () => {
|
||||
await setup({
|
||||
// test context options
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
// creates a fresh pinia and makes it active
|
||||
// so it's automatically picked up by any useStore() call
|
||||
// without having to pass it to it: `useStore(pinia)`
|
||||
setActivePinia(createPinia());
|
||||
});
|
||||
|
||||
test('should add a notification', () => {
|
||||
const notifyStore = useNotifyStore();
|
||||
const message = 'Test notification';
|
||||
const type = NotificationType.Info;
|
||||
|
||||
notifyStore.notify(message, type);
|
||||
|
||||
expect(notifyStore.notifications).toHaveLength(1);
|
||||
expect(notifyStore.notifications[0].message).toBe(message);
|
||||
expect(notifyStore.notifications[0].type).toBe(type);
|
||||
});
|
||||
|
||||
test('should add an Error notification', () => {
|
||||
const notifyStore = useNotifyStore();
|
||||
const error = new Error('Test error');
|
||||
const type = NotificationType.Error;
|
||||
|
||||
notifyStore.notify(error, type);
|
||||
|
||||
expect(notifyStore.notifications).toHaveLength(1);
|
||||
expect(notifyStore.notifications[0].message).toBe(error.message);
|
||||
expect(notifyStore.notifications[0].type).toBe(type);
|
||||
});
|
||||
|
||||
test('should remove a notification', () => {
|
||||
const notifyStore = useNotifyStore();
|
||||
const message = 'Test notification';
|
||||
const type = NotificationType.Info;
|
||||
|
||||
notifyStore.notify(message, type);
|
||||
const notification = notifyStore.notifications[0];
|
||||
|
||||
notifyStore.removeNotification(notification);
|
||||
|
||||
expect(notifyStore.notifications).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user