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
78 changes: 58 additions & 20 deletions app.mock.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
jest.mock('./validation/validateEmail', () => jest.fn(() => true))

const createApp = require('./app')
const request = require('supertest')
const validateUsername = require('./validation/validateUsername')
const validatePassword = require('./validation/validatePassword')

//Mock validateEmail to isolate tests
jest.mock('./validation/validateEmail', () => {
return jest.fn((email) => {
//Simulate real world simulation
if (!email || typeof email !== 'string') return false;
const re = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/i;
return re.test(email);
})
})

const validateEmail = require('./validation/validateEmail')

const app = createApp(validateUsername, validatePassword, validateEmail)

describe('given correct username and password', () => {
Expand All @@ -35,10 +27,16 @@ describe('given correct username and password', () => {
expect(response.body.userId).toBeDefined();
})

// test response content type?
// test response message
// test response user id value
// ...
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/)
})
})

describe('given incorrect or missing username and password', () => {
Expand All @@ -50,10 +48,50 @@ 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/)
})
})

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 = require('./validation/validateEmail')

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()
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 = require('./validation/validateEmail')

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 response message
// test that response does NOT have userId
// test incorrect username or password according to requirements
// test missing username or password
// ...
})
102 changes: 102 additions & 0 deletions app.mock.test.js.rej
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
diff a/app.mock.test.js b/app.mock.test.js (rejected hunks)
@@ -1,19 +1,11 @@
+jest.mock('./validation/validateEmail', () => jest.fn(() => true))
+
const createApp = require('./app')
const request = require('supertest')
const validateUsername = require('./validation/validateUsername')
const validatePassword = require('./validation/validatePassword')
-
-//Mock validateEmail to isolate tests
-jest.mock('./validation/validateEmail', () => {
- return jest.fn((email) => {
- //Simulate real world simulation
- if (!email || typeof email !== 'string') return false;
- const re = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/i;
- return re.test(email);
- })
-})
-
const validateEmail = require('./validation/validateEmail')
+
const app = createApp(validateUsername, validatePassword, validateEmail)

describe('given correct username and password', () => {
@@ -35,10 +27,16 @@ describe('given correct username and password', () => {
expect(response.body.userId).toBeDefined();
})

- // test response content type?
- // test response message
- // test response user id value
- // ...
+ 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/)
+ })
})

describe('given incorrect or missing username and password', () => {
@@ -50,10 +48,50 @@ 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/)
+ })
+})
+
+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 = require('./validation/validateEmail')
+
+ 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()
+ 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 = require('./validation/validateEmail')
+
+ 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 response message
- // test that response does NOT have userId
- // test incorrect username or password according to requirements
- // test missing username or password
- // ...
})
\ No newline at end of file
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()
})
})
Loading
Loading