From 66ac180f565ded201613109ab25bebace708e3e8 Mon Sep 17 00:00:00 2001 From: Kacper Michalik Date: Fri, 3 Nov 2023 14:51:14 +0100 Subject: [PATCH] test: basic unit tests --- .../src/nest/storage/storage.service.spec.ts | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/backend/src/nest/storage/storage.service.spec.ts b/packages/backend/src/nest/storage/storage.service.spec.ts index 0849a9d666..bbcabe7926 100644 --- a/packages/backend/src/nest/storage/storage.service.spec.ts +++ b/packages/backend/src/nest/storage/storage.service.spec.ts @@ -675,4 +675,48 @@ describe('StorageService', () => { await expect(storageService.deleteFilesFromChannel(messages)).resolves.not.toThrowError() }) }) + + describe('replicate certificatesRequests event', () => { + const replicatedEvent = async () => { + // @ts-ignore - Property 'certificates' is private + storageService.certificatesRequests.events.emit('replicated') + await new Promise(resolve => setTimeout(() => resolve(), 2000)) + } + + it('replicated event ', async () => { + await storageService.init(peerId) + const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList') + await replicatedEvent() + expect(spyOnUpdatePeersList).toBeCalledTimes(1) + }) + + it('2 replicated events - first not resolved ', async () => { + await storageService.init(peerId) + const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList') + await replicatedEvent() + await replicatedEvent() + expect(spyOnUpdatePeersList).toBeCalledTimes(1) + }) + + it('2 replicated events - first resolved ', async () => { + await storageService.init(peerId) + const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList') + await replicatedEvent() + await replicatedEvent() + storageService.resolveCsrReplicatedPromise(1) + await new Promise(resolve => setTimeout(() => resolve(), 500)) + expect(spyOnUpdatePeersList).toBeCalledTimes(2) + }) + + it('3 replicated events - no resolved promises', async () => { + await storageService.init(peerId) + const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList') + + await replicatedEvent() + await replicatedEvent() + await replicatedEvent() + + expect(spyOnUpdatePeersList).toBeCalledTimes(1) + }) + }) })