Skip to content

Commit

Permalink
deterministic id
Browse files Browse the repository at this point in the history
  • Loading branch information
develohpanda committed Jun 3, 2021
1 parent 10face2 commit c97d909
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
24 changes: 20 additions & 4 deletions packages/insomnia-app/app/ui/hooks/__tests__/space.test.ts
Expand Up @@ -41,7 +41,7 @@ describe('useRemoteSpaces', () => {
const vcs1 = new VCS(new MemoryDriver());
const vcs2 = new VCS(new MemoryDriver());
const team1 = { id: 'id1', name: 'team1' };
const team2 = { id: 'id1', name: 'team2' };
const team2 = { id: 'id2', name: 'team2' };
(vcs1.teams as jest.MockedFunction<typeof vcs1.teams>).mockResolvedValue([team1]);
(vcs2.teams as jest.MockedFunction<typeof vcs2.teams>).mockResolvedValue([team2]);

Expand Down Expand Up @@ -90,11 +90,27 @@ describe('useRemoteSpaces', () => {
expect(vcs.teams).toHaveBeenCalledTimes(1);
await expect(models.space.all()).resolves.toHaveLength(0);

(vcs.teams as jest.MockedFunction<typeof vcs.teams>).mockResolvedValue([{ id: 'id1', name: 'name' }, { id: 'id2', name: 'name' }]);
const team1 = { id: 'id1', name: 'team1' };
const team2 = { id: 'id2', name: 'team2' };
(vcs.teams as jest.MockedFunction<typeof vcs.teams>).mockResolvedValue([team1, team2]);

// Refresh multiple times
await act(() => result.current.refresh());
await act(() => result.current.refresh());

expect(vcs.teams).toHaveBeenCalledTimes(2);
await expect(models.space.all()).resolves.toHaveLength(2);
expect(vcs.teams).toHaveBeenCalledTimes(3);

const allSpaces = await models.space.all();
expect(allSpaces).toHaveLength(2);
expect(allSpaces).toEqual(expect.arrayContaining([
expect.objectContaining<Partial<Space>>({
remoteId: team1.id,
name: team1.name,
}),
expect.objectContaining<Partial<Space>>({
remoteId: team2.id,
name: team2.name,
}),
]));
});
});
5 changes: 4 additions & 1 deletion packages/insomnia-app/app/ui/hooks/space.ts
Expand Up @@ -14,7 +14,10 @@ export const useRemoteSpaces = (vcs?: VCS) => {
setLoading(true);

const teams = await vcs.teams();
const spaces = await Promise.all(teams.map(team => models.initModel<Space>(models.space.type, { remoteId: team.id, name: team.name })));
const spaces = await Promise.all(teams.map(team => models.initModel<Space>(
models.space.type,
{ _id: `${models.space.prefix}_${team.id}`, remoteId: team.id, name: team.name },
)));
await database.batchModifyDocs({ upsert: spaces });

setLoading(false);
Expand Down

0 comments on commit c97d909

Please sign in to comment.