Skip to content

Commit

Permalink
Clean up firebase mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
JHWelch committed Aug 6, 2023
1 parent 8c2b1ac commit 6c9fe56
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 69 deletions.
109 changes: 40 additions & 69 deletions __tests__/controllers/weekController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
})

Expand Down Expand Up @@ -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()
})

Expand Down
28 changes: 28 additions & 0 deletions __tests__/support/firebaseMock.ts
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit 6c9fe56

Please sign in to comment.