Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ describe('given correct username and password', () => {
expect(response.body.userId).toBeDefined();
})

test('returns correct message, userId value and content-type', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.body.userId).toBe('1')
expect(response.body.message).toBe('Valid User')
expect(response.headers['content-type']).toMatch(/application\/json/)
})

// test response content type?
// test response message
// test response user id value
Expand All @@ -40,10 +51,72 @@ describe('given incorrect or missing username and password', () => {
})
expect(response.statusCode).toBe(400)
})

test('returns error message and does not include userId', async () => {
const response = await request(app).post('/users').send({
username: 'user',
password: 'password',
email: 'not-an-email'
})
expect(response.body.error).toBe('Invalid User')
expect(response.body.userId).toBeUndefined()
expect(response.headers['content-type']).toMatch(/application\/json/)
})
// test response message
// test that response does NOT have userId
// test incorrect username or password according to requirements
// test missing username or password
// ...
})

describe('edge cases and performance-friendly tests', () => {
test('missing username returns 400 and no userId', async () => {
const validateUsernameMock = (u) => !!u && u.length >= 6
const validatePasswordMock = (p) => !!p && p.length >= 8
const validateEmailMock = jest.fn().mockReturnValue(true)

const appFast = createApp(validateUsernameMock, validatePasswordMock, validateEmailMock)

const response = await request(appFast).post('/users').send({
password: 'Password123',
email: 'student@example.com'
})

expect(response.statusCode).toBe(400)
expect(response.body.userId).toBeUndefined()
// app validates username, password and email in sequence, so email validator is called
expect(validateEmailMock).toHaveBeenCalled()
})

test('missing password returns 400 and no userId', async () => {
const validateUsernameMock = (u) => !!u && u.length >= 6
const validatePasswordMock = jest.fn().mockReturnValue(false)
const validateEmailMock = jest.fn().mockReturnValue(true)

const appFast = createApp(validateUsernameMock, validatePasswordMock, validateEmailMock)

const response = await request(appFast).post('/users').send({
username: 'Username',
email: 'student@example.com'
})

expect(response.statusCode).toBe(400)
expect(response.body.userId).toBeUndefined()
})

test('uses mocked email validator so tests stay fast', async () => {
const validateUsernameMock = jest.fn().mockReturnValue(true)
const validatePasswordMock = jest.fn().mockReturnValue(true)
const validateEmailMock = jest.fn().mockReturnValue(true)

const appFast = createApp(validateUsernameMock, validatePasswordMock, validateEmailMock)

const response = await request(appFast).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})

expect(response.statusCode).toBe(200)
expect(validateEmailMock).toHaveBeenCalled()
})
})
3 changes: 3 additions & 0 deletions test-runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
duration_ms:22216
duration_s:22.22
exit_code:0
Loading