Skip to content

Commit

Permalink
fix: broken user tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yong-jie committed Apr 13, 2021
1 parent 3b1ca77 commit 429b0be
Showing 1 changed file with 86 additions and 32 deletions.
118 changes: 86 additions & 32 deletions test/server/repositories/UserRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ const expectedUrl = {

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

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 @@ -57,6 +65,7 @@ describe('UserRepository', () => {
...user,
urls: undefined,
})
await expect(scope).toHaveBeenCalledWith('useMasterDb')
})

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

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

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

it('returns null if no user found', async () => {
findOne.mockReturnValue(null)
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 @@ -95,6 +114,7 @@ describe('UserRepository', () => {
...user,
urls: undefined,
})
await expect(scope).toBeCalledWith('useMasterDb')
})

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

it('directs findOrCreateWithEmail to User.findOrCreate', async () => {
const findOrCreate = jest.spyOn(userModelMock, 'findOrCreate')
await userRepo.findOrCreateWithEmail('user@agency.gov.sg')
expect(findOrCreate).toHaveBeenCalled()
describe('findOrCreateByEmail', () => {
const { scope } = userModelMock
const findOrCreate = jest.fn()

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])
await expect(
userRepo.findOrCreateWithEmail('user@agency.gov.sg'),
).resolves.toBe(userObject)
await expect(scope).toHaveBeenCalledWith('useMasterDb')
})
})

describe('findOneUrlForUser', () => {
Expand All @@ -135,23 +170,27 @@ describe('UserRepository', () => {
await expect(
userRepo.findOneUrlForUser(2, expectedUrl.shortUrl),
).resolves.toBeNull()
expect(scope).toHaveBeenCalledWith({
method: ['includeShortUrl', expectedUrl.shortUrl],
})
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{
method: ['includeShortUrl', expectedUrl.shortUrl],
},
])
})

it('returns url for user', async () => {
findOne.mockResolvedValue({
get: () => ({
Urls: [url],
}),
Urls: [url],
})
await expect(
userRepo.findOneUrlForUser(2, expectedUrl.shortUrl),
).resolves.toStrictEqual(expectedUrl)
expect(scope).toHaveBeenCalledWith({
method: ['includeShortUrl', expectedUrl.shortUrl],
})
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{
method: ['includeShortUrl', expectedUrl.shortUrl],
},
])
})
})

Expand All @@ -177,9 +216,12 @@ describe('UserRepository', () => {
).resolves.toStrictEqual(
expect.objectContaining({ id: user.id, email: user.email }),
)
expect(scope).toHaveBeenCalledWith({
method: ['includeShortUrl', expectedUrl.shortUrl],
})
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{
method: ['includeShortUrl', expectedUrl.shortUrl],
},
])
})
})

Expand Down Expand Up @@ -208,47 +250,59 @@ describe('UserRepository', () => {
await expect(userRepo.findUrlsForUser(conditions)).rejects.toBeInstanceOf(
NotFoundError,
)
expect(scope).toHaveBeenCalledWith({
method: ['urlsWithQueryConditions', conditions],
})
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{
method: ['urlsWithQueryConditions', conditions],
},
])
})

it('throws NotFoundError on findAndCountAll without user', async () => {
findAndCountAll.mockResolvedValue({ rows: [], count: 0 })
await expect(userRepo.findUrlsForUser(conditions)).rejects.toBeInstanceOf(
NotFoundError,
)
expect(scope).toHaveBeenCalledWith({
method: ['urlsWithQueryConditions', conditions],
})
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{
method: ['urlsWithQueryConditions', conditions],
},
])
})

it('returns empty result on user without urls', async () => {
const rows = [{ get: () => ({ Urls: [] }) }]
const rows = [{ Urls: [] }]
findAndCountAll.mockResolvedValue({ rows, count: rows.length })
await expect(userRepo.findUrlsForUser(conditions)).resolves.toStrictEqual(
{
urls: [],
count: 0,
},
)
expect(scope).toHaveBeenCalledWith({
method: ['urlsWithQueryConditions', conditions],
})
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{
method: ['urlsWithQueryConditions', conditions],
},
])
})

it('returns result on user with urls', async () => {
const rows = [{ get: () => ({ Urls: [url] }) }]
const rows = [{ Urls: [url] }]
findAndCountAll.mockResolvedValue({ rows, count: rows.length })
await expect(userRepo.findUrlsForUser(conditions)).resolves.toStrictEqual(
{
urls: [expectedUrl],
count: 1,
},
)
expect(scope).toHaveBeenCalledWith({
method: ['urlsWithQueryConditions', conditions],
})
expect(scope).toHaveBeenCalledWith([
{ method: ['useMasterDb'] },
{
method: ['urlsWithQueryConditions', conditions],
},
])
})
})
})

0 comments on commit 429b0be

Please sign in to comment.