mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
feat: add test environment for mock user (#1748)
This commit is contained in:
@@ -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';
|
||||
|
||||
39
apps/web/src/shared/__tests__/apis.spec.ts
Normal file
39
apps/web/src/shared/__tests__/apis.spec.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user