feat: add test environment for mock user (#1748)

This commit is contained in:
Himself65
2023-03-29 20:44:51 -05:00
committed by GitHub
parent 79128a3c3e
commit 94d284d841
8 changed files with 86 additions and 12 deletions
@@ -0,0 +1,39 @@
/**
* @vitest-environment node
*/
import { loginResponseSchema } from '@affine/workspace/affine/login';
import { beforeAll, describe, expect, it, vi } from 'vitest';
import type { affineApis as API } from '../apis';
let affineApis: typeof API;
beforeAll(async () => {
// @ts-expect-error
globalThis.window = undefined;
affineApis = (await import('../apis')).affineApis;
});
describe('apis', () => {
it('should defined', async () => {
expect(affineApis).toBeDefined();
expect(affineApis).toBe(globalThis.AFFINE_APIS);
});
it('login mock user', async () => {
const setItem = vi.fn((key: string, value: unknown) => {
expect(key).toBe('affine-login-v2');
expect(value).toBeTypeOf('string');
loginResponseSchema.parse(JSON.parse(value as string));
});
vi.stubGlobal('localStorage', {
setItem,
});
expect(globalThis.AFFINE_DEBUG).toBeDefined();
expect(globalThis.AFFINE_DEBUG.loginMockUser1).toBeTypeOf('function');
expect(globalThis.AFFINE_DEBUG.loginMockUser2).toBeTypeOf('function');
await (globalThis.AFFINE_DEBUG.loginMockUser1 as () => Promise<unknown>)();
await (globalThis.AFFINE_DEBUG.loginMockUser2 as () => Promise<unknown>)();
expect(setItem).toBeCalledTimes(2);
});
});