diff --git a/test/repositories/NotificationRepositorySharedTests.test.ts b/test/repositories/NotificationRepositorySharedTests.test.ts index 350ae53..282776a 100644 --- a/test/repositories/NotificationRepositorySharedTests.test.ts +++ b/test/repositories/NotificationRepositorySharedTests.test.ts @@ -1,30 +1,54 @@ -import { beforeEach, describe, expect, it, jest } from "@jest/globals"; +import { afterEach, beforeEach, describe, expect, it, jest } from "@jest/globals"; import { InMemoryNotificationRepository } from "../../src/repositories/InMemoryNotificationRepository"; import { NotificationEvent, NotificationRepository } from "../../src/repositories/NotificationRepository"; import { RedisNotificationRepository } from "../../src/repositories/RedisNotificationRepository"; +interface RepositoryHolder { + name: string; + factory(): Promise, + teardown(): Promise, +} + +class InMemoryRepositoryHolder implements RepositoryHolder { + name = 'InMemoryNotificationRepository'; + factory = async () => { + return new InMemoryNotificationRepository(); + } + teardown = async () => {} +} + +class RedisNotificationRepositoryHolder implements RepositoryHolder { + repo: RedisNotificationRepository | undefined; + + name = 'RedisNotificationRepository'; + factory = async () => { + this.repo = new RedisNotificationRepository(); + await this.repo.connect(); + return this.repo; + } + teardown = async () => { + if (this.repo) { + await this.repo.disconnect(); + } + } +} + const repositoryImplementations = [ - { - name: 'InMemoryNotificationRepository', - factory: async () => new InMemoryNotificationRepository(), - }, - { - name: 'RedisNotificationRepository', - factory: async () => { - const repo = new RedisNotificationRepository(); - await repo.connect(); - return repo; - }, - }, + new InMemoryRepositoryHolder(), + new RedisNotificationRepositoryHolder(), ] -describe.each(repositoryImplementations)('$name', ({ factory }) => { +describe.each(repositoryImplementations)('$name', (holder) => { let repo: NotificationRepository; beforeEach(async () => { - repo = await factory(); + repo = await holder.factory(); }); + afterEach(async () => { + await holder.teardown(); + }) + const notification = { deviceId: "device1", shuttleId: "shuttle1",