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

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