Skip to content

Commit

Permalink
fix: broken tests due to change in user model
Browse files Browse the repository at this point in the history
  • Loading branch information
yong-jie committed Apr 15, 2021
1 parent adb4f9c commit 17632fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ describe('StatisticsRepository', () => {
const userCount = 1
const clickCount = 3
const linkCount = 2
const scope = jest.spyOn(urlModelMock, 'scope')
const urlScope = jest.spyOn(urlModelMock, 'scope')
const userScope = jest.spyOn(userModelMock, 'scope')

beforeEach(async () => {
await new Promise<void>((resolve) => {
redisMockClient.flushall(() => resolve())
})
setSpy.mockClear()
mgetSpy.mockClear()
scope.mockReset()
urlScope.mockReset()
})

afterEach(async () => {
Expand Down Expand Up @@ -104,10 +105,8 @@ describe('StatisticsRepository', () => {

// @ts-ignore
mgetSpy.mockImplementation(raiseError)
Object.assign(userModelMock, {
unscoped: () => ({ count: () => userCount }),
})
scope.mockReturnValue({ count: () => linkCount })
userScope.mockReturnValue({ count: () => userCount })
urlScope.mockReturnValue({ count: () => linkCount })
Object.assign(urlClicksModelMock, {
unscoped: () => ({ sum: () => clickCount }),
})
Expand All @@ -132,10 +131,8 @@ describe('StatisticsRepository', () => {
callback(new Error('error'))
}

Object.assign(userModelMock, {
unscoped: () => ({ count: () => userCount }),
})
scope.mockReturnValue({ count: () => linkCount })
userScope.mockReturnValue({ count: () => userCount })
urlScope.mockReturnValue({ count: () => linkCount })
Object.assign(urlClicksModelMock, {
sum: () => clickCount,
})
Expand Down
43 changes: 14 additions & 29 deletions test/server/repositories/UserRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@ const expectedUrl = {

describe('UserRepository', () => {
describe('findById', () => {
const { scope } = userModelMock
const findByPk = jest.fn()
const findByPk = jest.spyOn(userModelMock, 'findByPk')

beforeEach(() => {
scope.mockReset()
scope.mockReturnValue({ findByPk })
findByPk.mockReset()
})

it('returns null if no user found', async () => {
findByPk.mockReturnValue(null)
await expect(userRepo.findById(2)).resolves.toBeNull()
await expect(scope).toHaveBeenCalledWith('useMasterDb')
})

it('returns user without urls if such a user found', async () => {
Expand All @@ -65,7 +61,6 @@ describe('UserRepository', () => {
...user,
urls: undefined,
})
await expect(scope).toHaveBeenCalledWith('useMasterDb')
})

it('returns user with urls if such a user found', async () => {
Expand All @@ -80,17 +75,13 @@ describe('UserRepository', () => {
email: user.email,
urls: [expectedUrl],
})
await expect(scope).toHaveBeenCalledWith('useMasterDb')
})
})

describe('findByEmail', () => {
const { scope } = userModelMock
const findOne = jest.fn()
const findOne = jest.spyOn(userModelMock, 'findOne')

beforeEach(() => {
scope.mockReset()
scope.mockReturnValue({ findOne })
findOne.mockReset()
})

Expand All @@ -99,7 +90,6 @@ describe('UserRepository', () => {
await expect(
userRepo.findByEmail('user@agency.gov.sg'),
).resolves.toBeNull()
await expect(scope).toBeCalledWith('useMasterDb')
})

it('returns user without urls if such a user found', async () => {
Expand All @@ -114,7 +104,6 @@ describe('UserRepository', () => {
...user,
urls: undefined,
})
await expect(scope).toBeCalledWith('useMasterDb')
})

it('returns user with urls if such a user found', async () => {
Expand All @@ -131,27 +120,23 @@ describe('UserRepository', () => {
email: user.email,
urls: [expectedUrl],
})
await expect(scope).toBeCalledWith('useMasterDb')
})
})

describe('findOrCreateByEmail', () => {
const { scope } = userModelMock
const findOrCreate = jest.fn()
const findOrCreate = jest.spyOn(userModelMock, 'findOrCreate')

beforeEach(() => {
scope.mockReset()
scope.mockReturnValue({ findOrCreate })
findOrCreate.mockReset()
})

it('directs findOrCreateWithEmail to User.findOrCreate', async () => {
const userObject = { email: 'user@agency.gov.sg' }
findOrCreate.mockResolvedValue([userObject, null])
const findOrCreate = jest.spyOn(userModelMock, 'findOrCreate')
const user = userModelMock.findOne()
findOrCreate.mockResolvedValue([user, null])
await expect(
userRepo.findOrCreateWithEmail('user@agency.gov.sg'),
).resolves.toBe(userObject)
await expect(scope).toHaveBeenCalledWith('useMasterDb')
).resolves.toBe(user)
})
})

Expand All @@ -171,7 +156,7 @@ describe('UserRepository', () => {
userRepo.findOneUrlForUser(2, expectedUrl.shortUrl),
).resolves.toBeNull()
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{ method: ['defaultScope'] },
{
method: ['includeShortUrl', expectedUrl.shortUrl],
},
Expand All @@ -186,7 +171,7 @@ describe('UserRepository', () => {
userRepo.findOneUrlForUser(2, expectedUrl.shortUrl),
).resolves.toStrictEqual(expectedUrl)
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{ method: ['defaultScope'] },
{
method: ['includeShortUrl', expectedUrl.shortUrl],
},
Expand Down Expand Up @@ -217,7 +202,7 @@ describe('UserRepository', () => {
expect.objectContaining({ id: user.id, email: user.email }),
)
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{ method: ['defaultScope'] },
{
method: ['includeShortUrl', expectedUrl.shortUrl],
},
Expand Down Expand Up @@ -251,7 +236,7 @@ describe('UserRepository', () => {
NotFoundError,
)
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{ method: ['defaultScope'] },
{
method: ['urlsWithQueryConditions', conditions],
},
Expand All @@ -264,7 +249,7 @@ describe('UserRepository', () => {
NotFoundError,
)
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{ method: ['defaultScope'] },
{
method: ['urlsWithQueryConditions', conditions],
},
Expand All @@ -281,7 +266,7 @@ describe('UserRepository', () => {
},
)
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{ method: ['defaultScope'] },
{
method: ['urlsWithQueryConditions', conditions],
},
Expand All @@ -298,7 +283,7 @@ describe('UserRepository', () => {
},
)
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{ method: ['defaultScope'] },
{
method: ['urlsWithQueryConditions', conditions],
},
Expand Down

0 comments on commit 17632fb

Please sign in to comment.