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

View File

@@ -1,6 +1,6 @@
import { getLoginStorage } from '@affine/workspace/affine/login';
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
import { atom } from 'jotai/index';
import { atom } from 'jotai';
import { BlockSuiteWorkspace } from '../../shared';
import { affineApis } from '../../shared/apis';

View File

@@ -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);
});
});

View File

@@ -18,11 +18,7 @@ if (typeof window === 'undefined') {
// This is for Server side rendering support
prefixUrl = new URL('http://' + config.serverAPI + '/').origin;
} else {
try {
new URL(serverAPI);
} catch (e) {
console.warn('serverAPI is not a valid URL', config.serverAPI);
}
prefixUrl = serverAPI;
}
} else {
const params = new URLSearchParams(window.location.search);
@@ -40,6 +36,41 @@ if (!globalThis.AFFINE_APIS) {
jotaiStore.set(currentAffineUserAtom, parseIdToken(response.token));
setLoginStorage(response);
};
const loginMockUser1 = async () => {
const user1 = await import('@affine-test/fixtures/built-in-user1.json');
const data = await fetch(prefixUrl + 'api/user/token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
type: 'DebugLoginUser',
email: user1.email,
password: user1.password,
}),
}).then(r => r.json());
setLogin(data);
};
const loginMockUser2 = async () => {
const user2 = await import('@affine-test/fixtures/built-in-user2.json');
const data = await fetch(prefixUrl + 'api/user/token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
type: 'DebugLoginUser',
email: user2.email,
password: user2.password,
}),
}).then(r => r.json());
setLogin(data);
};
globalThis.AFFINE_DEBUG = {
loginMockUser1,
loginMockUser2,
};
}
declare global {
@@ -50,6 +81,8 @@ declare global {
| undefined
| (ReturnType<typeof createUserApis> &
ReturnType<typeof createWorkspaceApis>);
// eslint-disable-next-line no-var
var AFFINE_DEBUG: Record<string, unknown>;
}
export { affineApis };