Skip to content

Commit

Permalink
chore: add jobRepository tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gweiying committed Dec 9, 2022
1 parent 9863aa8 commit 482c681
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/server/modules/job/repositories/JobRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class JobRepository implements interfaces.JobRepository {
}

create: (userId: number) => Promise<JobType> = async (userId) => {
const job = await Job.scope(['defaultScope']).create({ userId })
const job = await Job.create({ userId })
if (!job) throw new Error('Newly-created job is null')
return job
}
Expand Down
120 changes: 120 additions & 0 deletions src/server/modules/job/repositories/__tests__/JobRepository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* eslint-disable global-require */
import SequelizeMock from 'sequelize-mock'
import { JobStatusEnum } from '../../../../../shared/util/jobs'

export const sequelizeMock = new SequelizeMock()

export const jobModelMock = {
create: jest.fn(),
scope: jest.fn(),
findByPk: jest.fn(),
findOne: jest.fn(),
}

jest.mock('../../../../models/job', () => ({
Job: jobModelMock,
}))

describe('JobRepository', () => {
const userId = 2
const { JobRepository } = require('../JobRepository')
const repository = new JobRepository()

const create = jest.spyOn(jobModelMock, 'create')
const scope = jest.spyOn(jobModelMock, 'scope')
const findByPk = jest.spyOn(jobModelMock, 'findByPk')
const findOne = jest.spyOn(jobModelMock, 'findOne')

beforeEach(() => {
create.mockReset()
scope.mockReset()
findByPk.mockReset()
findOne.mockReset()
})

describe('findById', () => {
it('findById calls Job.findById and returns if it exists', async () => {
findByPk.mockImplementationOnce(() => ({ id: 1 }))
await expect(repository.findById(1)).resolves.toEqual({ id: 1 })
expect(findByPk).toBeCalledWith(1)
})
})

describe('create', () => {
it('create calls Job.create and returns created job', async () => {
create.mockImplementationOnce(() => ({ id: 1 }))
await expect(repository.create(userId)).resolves.toEqual({ id: 1 })
})
})

describe('update', () => {
const mockJob = {
id: 1,
update: jest.fn(),
userId: 3,
status: JobStatusEnum.InProgress,
}
const mockChanges = {
status: JobStatusEnum.Success,
}
const update = jest.spyOn(mockJob, 'update')
const findOne = jest.fn()

it('update uses default scope, passes to findOne, calls Job.update, and returns updated job', async () => {
scope.mockImplementationOnce(() => ({ findOne }))
findOne.mockResolvedValueOnce(mockJob)
update.mockImplementationOnce(() => ({ ...mockJob, ...mockChanges }))

await expect(
repository.update(mockJob, mockChanges),
).resolves.toStrictEqual({ ...mockJob, ...mockChanges })
expect(scope).toHaveBeenCalledWith(['defaultScope'])
expect(findOne).toHaveBeenCalledWith({ where: { id: 1 } })
expect(update).toHaveBeenCalledWith(mockChanges)
})
})

describe('findLatestJobForUser', () => {
const mockJob = {
id: 1,
update: jest.fn(),
userId: 3,
status: JobStatusEnum.InProgress,
}

it('findLatestJobForUser calls Job.findOne and returns output', async () => {
findOne.mockImplementationOnce(() => mockJob)
await expect(repository.findLatestJobForUser(userId)).resolves.toEqual(
mockJob,
)
expect(findOne).toBeCalledWith({
where: {
userId,
},
order: [['createdAt', 'DESC']],
})
})
})

describe('findJobForUser', () => {
const mockJob = {
id: 1,
update: jest.fn(),
userId: 3,
status: JobStatusEnum.InProgress,
}

it('findJobForUser calls Job.findOne and returns output', async () => {
findOne.mockImplementationOnce(() => mockJob)
await expect(repository.findJobForUser(userId, 1)).resolves.toEqual(
mockJob,
)
expect(findOne).toBeCalledWith({
where: {
userId,
id: 1,
},
})
})
})
})

0 comments on commit 482c681

Please sign in to comment.