From d4099b1aeae26c0724b95e8d2e414e2a0124af11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus-Peter=20H=C3=BCbner?= Date: Thu, 9 Mar 2023 21:06:52 +0100 Subject: [PATCH] add test for home- and foreign communities with order by id asc --- .../resolver/CommunityResolver.test.ts | 116 +++++++++++++++++- .../src/graphql/resolver/CommunityResolver.ts | 2 +- 2 files changed, 112 insertions(+), 6 deletions(-) diff --git a/backend/src/graphql/resolver/CommunityResolver.test.ts b/backend/src/graphql/resolver/CommunityResolver.test.ts index aae64a75a..dc59e1a13 100644 --- a/backend/src/graphql/resolver/CommunityResolver.test.ts +++ b/backend/src/graphql/resolver/CommunityResolver.test.ts @@ -42,6 +42,12 @@ describe('CommunityResolver', () => { ` describe('getCommunities', () => { + let homeCom1: DbCommunity + let homeCom2: DbCommunity + let homeCom3: DbCommunity + let foreignCom1: DbCommunity + let foreignCom2: DbCommunity + let foreignCom3: DbCommunity describe('with empty list', () => { it('returns no community entry', async () => { // const result: Community[] = await query({ query: getCommunities }) @@ -55,13 +61,8 @@ describe('CommunityResolver', () => { }) describe('only home-communities entries', () => { - let homeCom1: DbCommunity - let homeCom2: DbCommunity - let homeCom3: DbCommunity - beforeEach(async () => { jest.clearAllMocks() - await DbCommunity.clear() homeCom1 = DbCommunity.create() homeCom1.foreign = false @@ -130,5 +131,110 @@ describe('CommunityResolver', () => { }) }) }) + + describe('plus foreign-communities entries', () => { + beforeEach(async () => { + jest.clearAllMocks() + + foreignCom1 = DbCommunity.create() + foreignCom1.foreign = true + foreignCom1.publicKey = Buffer.from('publicKey-ForeignCommunity') + foreignCom1.apiVersion = '1_0' + foreignCom1.endPoint = 'http://remotehost' + foreignCom1.createdAt = new Date() + await DbCommunity.insert(foreignCom1) + + foreignCom2 = DbCommunity.create() + foreignCom2.foreign = true + foreignCom2.publicKey = Buffer.from('publicKey-ForeignCommunity') + foreignCom2.apiVersion = '1_1' + foreignCom2.endPoint = 'http://remotehost' + foreignCom2.createdAt = new Date() + await DbCommunity.insert(foreignCom2) + + foreignCom3 = DbCommunity.create() + foreignCom3.foreign = true + foreignCom3.publicKey = Buffer.from('publicKey-ForeignCommunity') + foreignCom3.apiVersion = '1_2' + foreignCom3.endPoint = 'http://remotehost' + foreignCom3.createdAt = new Date() + await DbCommunity.insert(foreignCom3) + }) + + it('returns 3x home and 3x foreign-community entries', async () => { + await expect(query({ query: getCommunities })).resolves.toMatchObject({ + data: { + getCommunities: [ + { + id: 1, + foreign: homeCom1.foreign, + publicKey: expect.stringMatching('publicKey-HomeCommunity'), + url: expect.stringMatching('http://localhost/api/1_0'), + lastAnnouncedAt: null, + verifiedAt: null, + lastErrorAt: null, + createdAt: homeCom1.createdAt.toISOString(), + updatedAt: null, + }, + { + id: 2, + foreign: homeCom2.foreign, + publicKey: expect.stringMatching('publicKey-HomeCommunity'), + url: expect.stringMatching('http://localhost/api/1_1'), + lastAnnouncedAt: null, + verifiedAt: null, + lastErrorAt: null, + createdAt: homeCom2.createdAt.toISOString(), + updatedAt: null, + }, + { + id: 3, + foreign: homeCom3.foreign, + publicKey: expect.stringMatching('publicKey-HomeCommunity'), + url: expect.stringMatching('http://localhost/api/2_0'), + lastAnnouncedAt: null, + verifiedAt: null, + lastErrorAt: null, + createdAt: homeCom3.createdAt.toISOString(), + updatedAt: null, + }, + { + id: 4, + foreign: foreignCom1.foreign, + publicKey: expect.stringMatching('publicKey-ForeignCommunity'), + url: expect.stringMatching('http://remotehost/api/1_0'), + lastAnnouncedAt: null, + verifiedAt: null, + lastErrorAt: null, + createdAt: foreignCom1.createdAt.toISOString(), + updatedAt: null, + }, + { + id: 5, + foreign: foreignCom2.foreign, + publicKey: expect.stringMatching('publicKey-ForeignCommunity'), + url: expect.stringMatching('http://remotehost/api/1_1'), + lastAnnouncedAt: null, + verifiedAt: null, + lastErrorAt: null, + createdAt: foreignCom2.createdAt.toISOString(), + updatedAt: null, + }, + { + id: 6, + foreign: foreignCom3.foreign, + publicKey: expect.stringMatching('publicKey-ForeignCommunity'), + url: expect.stringMatching('http://remotehost/api/1_2'), + lastAnnouncedAt: null, + verifiedAt: null, + lastErrorAt: null, + createdAt: foreignCom3.createdAt.toISOString(), + updatedAt: null, + }, + ], + }, + }) + }) + }) }) }) diff --git a/backend/src/graphql/resolver/CommunityResolver.ts b/backend/src/graphql/resolver/CommunityResolver.ts index 8250170c6..1bb0d3c64 100644 --- a/backend/src/graphql/resolver/CommunityResolver.ts +++ b/backend/src/graphql/resolver/CommunityResolver.ts @@ -11,7 +11,7 @@ export class CommunityResolver { @Query(() => [Community]) async getCommunities(): Promise { const comList: Community[] = [] - const dbCommunities: DbCommunity[] = await DbCommunity.find() + const dbCommunities: DbCommunity[] = await DbCommunity.find({ order: { id: 'ASC' } }) dbCommunities.forEach(async function (dbCom) { const com = new Community(dbCom) comList.push(com)