Skip to content

Commit

Permalink
feat: add frontend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldziher committed Oct 13, 2023
1 parent 22c356f commit 5328b2f
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions frontend/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './applications-api';
export * from './fetcher';
export * from './project-users-api';
export * from './projects-api';
export * from './prompt-config-api';
export * from './tokens-api';
148 changes: 148 additions & 0 deletions frontend/tests/api/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
ApplicationFactory,
ProjectFactory,
ProjectUserAccountFactory,
PromptConfigFactory,
TokenFactory,
} from 'tests/factories';
Expand All @@ -25,6 +26,11 @@ import {
handleUpdateProject,
handleUpdatePromptConfig,
} from '@/api';
import {
handleAddUserToProject,
handleRemoveUserFromProject,
handleUpdateUserToPermission,
} from '@/api/project-users-api';
import { HttpMethod } from '@/constants';

describe('API tests', () => {
Expand Down Expand Up @@ -570,5 +576,147 @@ describe('API tests', () => {
});
});
});

describe('Project Users', () => {
describe('handleRetrieveProjectUsers', () => {
it('returns a list of project users', async () => {
const project = await ProjectFactory.build();
const application = await ApplicationFactory.build();
const userAccounts =
await ProjectUserAccountFactory.batch(2);

mockFetch.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(userAccounts),
});

const data = await handleRetrieveTokens({
projectId: project.id,
applicationId: application.id,
});

expect(data).toEqual(userAccounts);
expect(mockFetch).toHaveBeenCalledWith(
new URL(
`http://www.example.com/v1/projects/${project.id}/users/`,
),
{
headers: {
'Authorization': 'Bearer test_token',
'Content-Type': 'application/json',
'X-Request-Id': expect.any(String),
},
method: HttpMethod.Get,
},
);
});
});
describe('handleAddUserToProject', () => {
it('returns a project user', async () => {
const project = await ProjectFactory.build();
const userAccount = await ProjectUserAccountFactory.build();

mockFetch.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(userAccount),
});

const body = {
email: userAccount.email,
permission: userAccount.permission,
};

const data = await handleAddUserToProject({
projectId: project.id,
data: body,
});

expect(data).toEqual(userAccount);
expect(mockFetch).toHaveBeenCalledWith(
new URL(
`http://www.example.com/v1/projects/${project.id}/users/`,
),
{
headers: {
'Authorization': 'Bearer test_token',
'Content-Type': 'application/json',
'X-Request-Id': expect.any(String),
},
method: HttpMethod.Post,
body: JSON.stringify(body),
},
);
});
});
describe('handleUpdateUserToPermission', () => {
it('returns a project user', async () => {
const project = await ProjectFactory.build();
const application = await ApplicationFactory.build();
const userAccount = await ProjectUserAccountFactory.build();

mockFetch.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(userAccount),
});

const body = {
userId: userAccount.id,
permission: userAccount.permission,
};

const data = await handleUpdateUserToPermission({
projectId: project.id,
data: body,
});

expect(data).toEqual(userAccount);
expect(mockFetch).toHaveBeenCalledWith(
new URL(
`http://www.example.com/v1/projects/${project.id}/users/${userAccount.id}/`,
),
{
headers: {
'Authorization': 'Bearer test_token',
'Content-Type': 'application/json',
'X-Request-Id': expect.any(String),
},
method: HttpMethod.Patch,
body: JSON.stringify(body),
},
);
});
});
describe('handleDeleteProjectUser', () => {
it('returns undefined', async () => {
const project = await ProjectFactory.build();
const userAccount = await ProjectUserAccountFactory.build();

mockFetch.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(),
});

const data = await handleRemoveUserFromProject({
projectId: project.id,
userId: userAccount.id,
});

expect(data).toBeUndefined();
expect(mockFetch).toHaveBeenCalledWith(
new URL(
`http://www.example.com/v1/projects/${project.id}/users/${userAccount.id}/`,
),
{
headers: {
'Authorization': 'Bearer test_token',
'Content-Type': 'application/json',
'X-Request-Id': expect.any(String),
},
method: HttpMethod.Delete,
},
);
});
});
});
});
});
14 changes: 14 additions & 0 deletions frontend/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ModelVendor,
OpenAIPromptMessage,
Project,
ProjectUserAccount,
PromptConfig,
Token,
} from '@/types';
Expand Down Expand Up @@ -67,3 +68,16 @@ export const TokenFactory = new TypeFactory<Token>(() => ({
name: faker.lorem.words(),
createdAt: faker.date.past().toISOString(),
}));

export const ProjectUserAccountFactory = new TypeFactory<ProjectUserAccount>(
() => ({
id: faker.string.uuid(),
displayName: faker.person.fullName(),
email: faker.internet.email(),
firebaseId: faker.string.uuid(),
phoneNumber: faker.phone.number(),
photoUrl: faker.internet.url(),
createdAt: faker.date.past().toISOString(),
permission: AccessPermission.ADMIN,
}),
);

0 comments on commit 5328b2f

Please sign in to comment.