From 6c9fe562d2f51530b4f0a0381e7e2c45c75b8f88 Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Sun, 6 Aug 2023 17:03:24 -0500 Subject: [PATCH] Clean up firebase mocks --- __tests__/controllers/weekController.spec.ts | 109 +++++++------------ __tests__/support/firebaseMock.ts | 28 +++++ 2 files changed, 68 insertions(+), 69 deletions(-) create mode 100644 __tests__/support/firebaseMock.ts diff --git a/__tests__/controllers/weekController.spec.ts b/__tests__/controllers/weekController.spec.ts index f6dce3c3..574d48c5 100644 --- a/__tests__/controllers/weekController.spec.ts +++ b/__tests__/controllers/weekController.spec.ts @@ -3,11 +3,8 @@ import WeekController from '../../src/controllers/weekController' import { Request } from 'express' import { getMockReq, getMockRes } from '@jest-mock/express' import setupFirestore from '../../src/config/firestore' -import { - Firestore, - getDocs, - Timestamp, -} from 'firebase/firestore' +import { Firestore } from 'firebase/firestore' +import { FirebaseMock } from '../support/firebaseMock' const { res, mockClear } = getMockRes() @@ -28,38 +25,25 @@ describe('index', () => { let req: Request beforeEach(() => { - firestore = setupFirestore(); - (getDocs as unknown as jest.Mock).mockImplementation(() => { - return { - docs: [ - { - data: () => ({ - date: Timestamp.fromDate(new Date('2021-01-01')), - id: 'id1', - isSkipped: false, - movies: [], - theme: 'theme1', - }), - }, { - data: () => ({ - date: Timestamp.fromDate(new Date('2021-01-08')), - id: 'id2', - isSkipped: false, - movies: [], - theme: 'theme2', - }), - }, { - data: () => ({ - date: Timestamp.fromDate(new Date('2021-01-15')), - id: 'id3', - isSkipped: false, - movies: [], - theme: 'theme3', - }), - }, - ], - } - }) + firestore = setupFirestore() + FirebaseMock.mockWeeks([ + { + date: new Date('2021-01-01'), + id: 'id1', + isSkipped: false, + theme: 'theme1', + }, { + date: new Date('2021-01-08'), + id: 'id2', + isSkipped: false, + theme: 'theme2', + }, { + date: new Date('2021-01-15'), + id: 'id3', + isSkipped: false, + theme: 'theme3', + }, + ]) req = getMockReq() }) @@ -95,38 +79,25 @@ describe('index', () => { let req: Request beforeEach(() => { - firestore = setupFirestore(); - (getDocs as unknown as jest.Mock).mockImplementation(() => { - return { - docs: [ - { - data: () => ({ - date: Timestamp.fromDate(new Date('2021-01-01')), - id: 'id1', - isSkipped: false, - movies: [], - theme: 'theme1', - }), - }, { - data: () => ({ - date: Timestamp.fromDate(new Date('2021-01-08')), - id: 'id2', - isSkipped: false, - movies: [], - theme: 'theme2', - }), - }, { - data: () => ({ - date: Timestamp.fromDate(new Date('2021-01-15')), - id: 'id3', - isSkipped: false, - movies: [], - theme: 'theme3', - }), - }, - ], - } - }) + firestore = setupFirestore() + FirebaseMock.mockWeeks([ + { + date: new Date('2021-01-01'), + id: 'id1', + isSkipped: false, + theme: 'theme1', + }, { + date: new Date('2021-01-08'), + id: 'id2', + isSkipped: false, + theme: 'theme2', + }, { + date: new Date('2021-01-15'), + id: 'id3', + isSkipped: false, + theme: 'theme3', + }, + ]) req = getMockReq() }) diff --git a/__tests__/support/firebaseMock.ts b/__tests__/support/firebaseMock.ts new file mode 100644 index 00000000..03a17cb2 --- /dev/null +++ b/__tests__/support/firebaseMock.ts @@ -0,0 +1,28 @@ +import { getDocs, Timestamp } from 'firebase/firestore' +import { jest } from '@jest/globals' + + +export class FirebaseMock { + static mockWeeks (weeks: FirebaseWeek[]) { + (getDocs as unknown as jest.Mock).mockImplementation(() => { + return { + docs: weeks.map((week) => ({ + data: () => ({ + id: week.id, + theme: week.theme, + date: Timestamp.fromDate(week.date), + isSkipped: week.isSkipped, + movies: [], + }), + })), + } + }) + } +} + +type FirebaseWeek = { + date: Date, + id: string, + isSkipped: boolean, + theme: string, +}