From db15c4145827693b8edc0a1f93f23da2665e834b Mon Sep 17 00:00:00 2001 From: Janishar Ali Date: Sat, 6 Jan 2024 02:16:20 +0530 Subject: [PATCH] remove jest deprecated functions --- tests/auth/apikey/unit.test.ts | 10 +-- tests/auth/authentication/unit.test.ts | 30 +++---- tests/auth/authorization/unit.test.ts | 12 +-- tests/core/jwt/unit.test.ts | 14 +-- tests/routes/access/login/integration.test.ts | 66 +++++++------- tests/routes/access/login/unit.test.ts | 66 +++++++------- tests/routes/access/signup/unit.test.ts | 66 +++++++------- tests/routes/blog/index/unit.test.ts | 54 ++++++------ tests/routes/blog/writer/unit.test.ts | 88 +++++++++---------- 9 files changed, 203 insertions(+), 203 deletions(-) diff --git a/tests/auth/apikey/unit.test.ts b/tests/auth/apikey/unit.test.ts index f0475e6..63f814f 100644 --- a/tests/auth/apikey/unit.test.ts +++ b/tests/auth/apikey/unit.test.ts @@ -15,7 +15,7 @@ describe('apikey validation', () => { it('Should response with 400 if x-api-key header is not passed', async () => { const response = await request.get(endpoint).timeout(2000); expect(response.status).toBe(400); - expect(mockFindApiKey).not.toBeCalled(); + expect(mockFindApiKey).not.toHaveBeenCalled(); }); it('Should response with 403 if wrong x-api-key header is passed', async () => { @@ -25,8 +25,8 @@ describe('apikey validation', () => { .set('x-api-key', wrongApiKey) .timeout(2000); expect(response.status).toBe(403); - expect(mockFindApiKey).toBeCalledTimes(1); - expect(mockFindApiKey).toBeCalledWith(wrongApiKey); + expect(mockFindApiKey).toHaveBeenCalledTimes(1); + expect(mockFindApiKey).toHaveBeenCalledWith(wrongApiKey); }); it('Should response with 404 if correct x-api-key header is passed and when route is not handelled', async () => { @@ -35,7 +35,7 @@ describe('apikey validation', () => { .set('x-api-key', API_KEY) .timeout(2000); expect(response.status).toBe(404); - expect(mockFindApiKey).toBeCalledTimes(1); - expect(mockFindApiKey).toBeCalledWith(API_KEY); + expect(mockFindApiKey).toHaveBeenCalledTimes(1); + expect(mockFindApiKey).toHaveBeenCalledWith(API_KEY); }); }); diff --git a/tests/auth/authentication/unit.test.ts b/tests/auth/authentication/unit.test.ts index cd5ed46..f8d08be 100644 --- a/tests/auth/authentication/unit.test.ts +++ b/tests/auth/authentication/unit.test.ts @@ -29,7 +29,7 @@ describe('authentication validation', () => { const response = await addHeaders(request.get(endpoint)); expect(response.status).toBe(400); expect(response.body.message).toMatch(/authorization/); - expect(getAccessTokenSpy).not.toBeCalled(); + expect(getAccessTokenSpy).not.toHaveBeenCalled(); }); it('Should response with 400 if Authorization header do not have Bearer', async () => { @@ -39,7 +39,7 @@ describe('authentication validation', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/authorization/); - expect(getAccessTokenSpy).not.toBeCalled(); + expect(getAccessTokenSpy).not.toHaveBeenCalled(); }); it('Should response with 401 if wrong Authorization header is provided', async () => { @@ -49,12 +49,12 @@ describe('authentication validation', () => { ); expect(response.status).toBe(401); expect(response.body.message).toMatch(/token/i); - expect(getAccessTokenSpy).toBeCalledTimes(1); - expect(getAccessTokenSpy).toBeCalledWith('Bearer 123'); - expect(getAccessTokenSpy).toReturnWith('123'); - expect(mockJwtValidate).toBeCalledTimes(1); - expect(mockJwtValidate).toBeCalledWith('123'); - expect(mockUserFindById).not.toBeCalled(); + expect(getAccessTokenSpy).toHaveBeenCalledTimes(1); + expect(getAccessTokenSpy).toHaveBeenCalledWith('Bearer 123'); + expect(getAccessTokenSpy).toHaveReturnedWith('123'); + expect(mockJwtValidate).toHaveBeenCalledTimes(1); + expect(mockJwtValidate).toHaveBeenCalledWith('123'); + expect(mockUserFindById).not.toHaveBeenCalled(); }); it('Should response with 404 if correct Authorization header is provided', async () => { @@ -62,12 +62,12 @@ describe('authentication validation', () => { expect(response.body.message).not.toMatch(/not registered/); expect(response.body.message).not.toMatch(/token/i); expect(response.status).toBe(404); - expect(getAccessTokenSpy).toBeCalledTimes(1); - expect(getAccessTokenSpy).toBeCalledWith(`Bearer ${ACCESS_TOKEN}`); - expect(getAccessTokenSpy).toReturnWith(ACCESS_TOKEN); - expect(mockJwtValidate).toBeCalledTimes(1); - expect(mockJwtValidate).toBeCalledWith(ACCESS_TOKEN); - expect(mockUserFindById).toBeCalledTimes(1); - expect(mockKeystoreFindForKey).toBeCalledTimes(1); + expect(getAccessTokenSpy).toHaveBeenCalledTimes(1); + expect(getAccessTokenSpy).toHaveBeenCalledWith(`Bearer ${ACCESS_TOKEN}`); + expect(getAccessTokenSpy).toHaveReturnedWith(ACCESS_TOKEN); + expect(mockJwtValidate).toHaveBeenCalledTimes(1); + expect(mockJwtValidate).toHaveBeenCalledWith(ACCESS_TOKEN); + expect(mockUserFindById).toHaveBeenCalledTimes(1); + expect(mockKeystoreFindForKey).toHaveBeenCalledTimes(1); }); }); diff --git a/tests/auth/authorization/unit.test.ts b/tests/auth/authorization/unit.test.ts index 166fede..807d666 100644 --- a/tests/auth/authorization/unit.test.ts +++ b/tests/auth/authorization/unit.test.ts @@ -27,9 +27,9 @@ describe('authentication validation for editor', () => { const response = await addAuthHeaders(request.get(endpoint)); expect(response.status).toBe(401); expect(response.body.message).toMatch(/denied/); - expect(mockRoleRepoFindByCodes).toBeCalledTimes(1); - expect(mockUserFindById).toBeCalledTimes(1); - expect(mockRoleRepoFindByCodes).toBeCalledWith([ + expect(mockRoleRepoFindByCodes).toHaveBeenCalledTimes(1); + expect(mockUserFindById).toHaveBeenCalledTimes(1); + expect(mockRoleRepoFindByCodes).toHaveBeenCalledWith([ RoleCode.ADMIN, RoleCode.EDITOR, ]); @@ -51,8 +51,8 @@ describe('authentication validation for writer', () => { EDITOR_ACCESS_TOKEN, ); expect(response.status).toBe(404); - expect(mockRoleRepoFindByCodes).toBeCalledTimes(1); - expect(mockUserFindById).toBeCalledTimes(1); - expect(mockRoleRepoFindByCodes).toBeCalledWith([RoleCode.WRITER]); + expect(mockRoleRepoFindByCodes).toHaveBeenCalledTimes(1); + expect(mockUserFindById).toHaveBeenCalledTimes(1); + expect(mockRoleRepoFindByCodes).toHaveBeenCalledWith([RoleCode.WRITER]); }); }); diff --git a/tests/core/jwt/unit.test.ts b/tests/core/jwt/unit.test.ts index 8a20558..f6a4928 100644 --- a/tests/core/jwt/unit.test.ts +++ b/tests/core/jwt/unit.test.ts @@ -20,7 +20,7 @@ describe('JWT class tests', () => { expect(e).toBeInstanceOf(BadTokenError); } - expect(readFileSpy).toBeCalledTimes(1); + expect(readFileSpy).toHaveBeenCalledTimes(1); }); it('Should generate a token for JWT.encode', async () => { @@ -28,7 +28,7 @@ describe('JWT class tests', () => { const token = await JWT.encode(payload); expect(typeof token).toBe('string'); - expect(readFileSpy).toBeCalledTimes(1); + expect(readFileSpy).toHaveBeenCalledTimes(1); }); it('Should decode a valid token for JWT.decode', async () => { @@ -37,7 +37,7 @@ describe('JWT class tests', () => { const decoded = await JWT.decode(token); expect(decoded).toMatchObject(payload); - expect(readFileSpy).toBeCalledTimes(2); + expect(readFileSpy).toHaveBeenCalledTimes(2); }); it('Should parse an expired token for JWT.decode', async () => { @@ -55,7 +55,7 @@ describe('JWT class tests', () => { const decoded = await JWT.decode(token); expect(decoded).toMatchObject(payload); - expect(readFileSpy).toBeCalledTimes(2); + expect(readFileSpy).toHaveBeenCalledTimes(2); }); it('Should throw error for invalid token in JWT.validate', async () => { @@ -65,7 +65,7 @@ describe('JWT class tests', () => { expect(e).toBeInstanceOf(BadTokenError); } - expect(readFileSpy).toBeCalledTimes(1); + expect(readFileSpy).toHaveBeenCalledTimes(1); }); it('Should validate a valid token for JWT.validate', async () => { @@ -74,7 +74,7 @@ describe('JWT class tests', () => { const decoded = await JWT.validate(token); expect(decoded).toMatchObject(payload); - expect(readFileSpy).toBeCalledTimes(2); + expect(readFileSpy).toHaveBeenCalledTimes(2); }); it('Should validate a token expiry for JWT.validate', async () => { @@ -94,6 +94,6 @@ describe('JWT class tests', () => { } catch (e) { expect(e).toBeInstanceOf(TokenExpiredError); } - expect(readFileSpy).toBeCalledTimes(2); + expect(readFileSpy).toHaveBeenCalledTimes(2); }); }); diff --git a/tests/routes/access/login/integration.test.ts b/tests/routes/access/login/integration.test.ts index afa6dc0..9750d08 100644 --- a/tests/routes/access/login/integration.test.ts +++ b/tests/routes/access/login/integration.test.ts @@ -57,10 +57,10 @@ describe('Login basic route', () => { it('Should send error when empty body is sent', async () => { const response = await addHeaders(request.post(endpoint), apikey); expect(response.status).toBe(400); - expect(userFindByEmailSpy).not.toBeCalled(); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(keystoreCreateSpy).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(userFindByEmailSpy).not.toHaveBeenCalled(); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(keystoreCreateSpy).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when email is only sent', async () => { @@ -70,10 +70,10 @@ describe('Login basic route', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/password/); - expect(userFindByEmailSpy).not.toBeCalled(); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(keystoreCreateSpy).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(userFindByEmailSpy).not.toHaveBeenCalled(); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(keystoreCreateSpy).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when password is only sent', async () => { @@ -83,10 +83,10 @@ describe('Login basic route', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/email/); - expect(userFindByEmailSpy).not.toBeCalled(); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(keystoreCreateSpy).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(userFindByEmailSpy).not.toHaveBeenCalled(); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(keystoreCreateSpy).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when email is not valid format', async () => { @@ -96,10 +96,10 @@ describe('Login basic route', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/valid email/); - expect(userFindByEmailSpy).not.toBeCalled(); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(keystoreCreateSpy).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(userFindByEmailSpy).not.toHaveBeenCalled(); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(keystoreCreateSpy).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when password is not valid format', async () => { @@ -113,10 +113,10 @@ describe('Login basic route', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/password length/); expect(response.body.message).toMatch(/6 char/); - expect(userFindByEmailSpy).not.toBeCalled(); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(keystoreCreateSpy).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(userFindByEmailSpy).not.toHaveBeenCalled(); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(keystoreCreateSpy).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when user not registered for email', async () => { @@ -129,10 +129,10 @@ describe('Login basic route', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/not registered/); - expect(userFindByEmailSpy).toBeCalledTimes(1); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(keystoreCreateSpy).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(userFindByEmailSpy).toHaveBeenCalledTimes(1); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(keystoreCreateSpy).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error for wrong password', async () => { @@ -145,10 +145,10 @@ describe('Login basic route', () => { ); expect(response.status).toBe(401); expect(response.body.message).toMatch(/authentication failure/i); - expect(userFindByEmailSpy).toBeCalledTimes(1); - expect(bcryptCompareSpy).toBeCalledTimes(1); - expect(keystoreCreateSpy).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(userFindByEmailSpy).toHaveBeenCalledTimes(1); + expect(bcryptCompareSpy).toHaveBeenCalledTimes(1); + expect(keystoreCreateSpy).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send success response for correct credentials', async () => { @@ -172,12 +172,12 @@ describe('Login basic route', () => { expect(response.body.data.tokens).toHaveProperty('accessToken'); expect(response.body.data.tokens).toHaveProperty('refreshToken'); - expect(userFindByEmailSpy).toBeCalledTimes(1); - expect(keystoreCreateSpy).toBeCalledTimes(1); - expect(bcryptCompareSpy).toBeCalledTimes(1); - expect(createTokensSpy).toBeCalledTimes(1); + expect(userFindByEmailSpy).toHaveBeenCalledTimes(1); + expect(keystoreCreateSpy).toHaveBeenCalledTimes(1); + expect(bcryptCompareSpy).toHaveBeenCalledTimes(1); + expect(createTokensSpy).toHaveBeenCalledTimes(1); - expect(bcryptCompareSpy).toBeCalledWith(password, user.password); + expect(bcryptCompareSpy).toHaveBeenCalledWith(password, user.password); }); }); diff --git a/tests/routes/access/login/unit.test.ts b/tests/routes/access/login/unit.test.ts index e88cadf..fed8401 100644 --- a/tests/routes/access/login/unit.test.ts +++ b/tests/routes/access/login/unit.test.ts @@ -30,10 +30,10 @@ describe('Login basic route', () => { it('Should send error when empty body is sent', async () => { const response = await addHeaders(request.post(endpoint)); expect(response.status).toBe(400); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(mockKeystoreCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(mockKeystoreCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when email is only sent', async () => { @@ -42,10 +42,10 @@ describe('Login basic route', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/password/); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(mockKeystoreCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(mockKeystoreCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when password is only sent', async () => { @@ -54,10 +54,10 @@ describe('Login basic route', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/email/); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(mockKeystoreCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(mockKeystoreCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when email is not valid format', async () => { @@ -66,10 +66,10 @@ describe('Login basic route', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/valid email/); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(mockKeystoreCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(mockKeystoreCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when password is not valid format', async () => { @@ -82,10 +82,10 @@ describe('Login basic route', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/password length/); expect(response.body.message).toMatch(/6 char/); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(mockKeystoreCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(mockKeystoreCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when user not registered for email', async () => { @@ -97,10 +97,10 @@ describe('Login basic route', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/not registered/); - expect(mockUserFindByEmail).toBeCalledTimes(1); - expect(bcryptCompareSpy).not.toBeCalled(); - expect(mockKeystoreCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).toHaveBeenCalledTimes(1); + expect(bcryptCompareSpy).not.toHaveBeenCalled(); + expect(mockKeystoreCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error for wrong password', async () => { @@ -112,10 +112,10 @@ describe('Login basic route', () => { ); expect(response.status).toBe(401); expect(response.body.message).toMatch(/authentication failure/i); - expect(mockUserFindByEmail).toBeCalledTimes(1); - expect(bcryptCompareSpy).toBeCalledTimes(1); - expect(mockKeystoreCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).toHaveBeenCalledTimes(1); + expect(bcryptCompareSpy).toHaveBeenCalledTimes(1); + expect(mockKeystoreCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send success response for correct credentials', async () => { @@ -138,11 +138,11 @@ describe('Login basic route', () => { expect(response.body.data.tokens).toHaveProperty('accessToken'); expect(response.body.data.tokens).toHaveProperty('refreshToken'); - expect(mockUserFindByEmail).toBeCalledTimes(1); - expect(mockKeystoreCreate).toBeCalledTimes(1); - expect(bcryptCompareSpy).toBeCalledTimes(1); - expect(createTokensSpy).toBeCalledTimes(1); + expect(mockUserFindByEmail).toHaveBeenCalledTimes(1); + expect(mockKeystoreCreate).toHaveBeenCalledTimes(1); + expect(bcryptCompareSpy).toHaveBeenCalledTimes(1); + expect(createTokensSpy).toHaveBeenCalledTimes(1); - expect(bcryptCompareSpy).toBeCalledWith(USER_PASSWORD, USER_PASSWORD_HASH); + expect(bcryptCompareSpy).toHaveBeenCalledWith(USER_PASSWORD, USER_PASSWORD_HASH); }); }); diff --git a/tests/routes/access/signup/unit.test.ts b/tests/routes/access/signup/unit.test.ts index 953a1b3..5a4ac51 100644 --- a/tests/routes/access/signup/unit.test.ts +++ b/tests/routes/access/signup/unit.test.ts @@ -36,10 +36,10 @@ describe('Signup basic route', () => { it('Should send error when empty body is sent', async () => { const response = await addHeaders(request.post(endpoint)); expect(response.status).toBe(400); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptHashSpy).not.toBeCalled(); - expect(mockUserCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptHashSpy).not.toHaveBeenCalled(); + expect(mockUserCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when email is not sent', async () => { @@ -53,10 +53,10 @@ describe('Signup basic route', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/email/); expect(response.body.message).toMatch(/required/); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptHashSpy).not.toBeCalled(); - expect(mockUserCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptHashSpy).not.toHaveBeenCalled(); + expect(mockUserCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when password is not sent', async () => { @@ -70,10 +70,10 @@ describe('Signup basic route', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/password/); expect(response.body.message).toMatch(/required/); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptHashSpy).not.toBeCalled(); - expect(mockUserCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptHashSpy).not.toHaveBeenCalled(); + expect(mockUserCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when name is not sent', async () => { @@ -87,10 +87,10 @@ describe('Signup basic route', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/name/); expect(response.body.message).toMatch(/required/); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptHashSpy).not.toBeCalled(); - expect(mockUserCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptHashSpy).not.toHaveBeenCalled(); + expect(mockUserCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when email is not valid format', async () => { @@ -104,10 +104,10 @@ describe('Signup basic route', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/valid email/); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptHashSpy).not.toBeCalled(); - expect(mockUserCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptHashSpy).not.toHaveBeenCalled(); + expect(mockUserCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when password is not valid format', async () => { @@ -122,10 +122,10 @@ describe('Signup basic route', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/password length/); expect(response.body.message).toMatch(/6 char/); - expect(mockUserFindByEmail).not.toBeCalled(); - expect(bcryptHashSpy).not.toBeCalled(); - expect(mockUserCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).not.toHaveBeenCalled(); + expect(bcryptHashSpy).not.toHaveBeenCalled(); + expect(mockUserCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send error when user is registered for email', async () => { @@ -140,10 +140,10 @@ describe('Signup basic route', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/already registered/); - expect(mockUserFindByEmail).toBeCalledTimes(1); - expect(bcryptHashSpy).not.toBeCalled(); - expect(mockUserCreate).not.toBeCalled(); - expect(createTokensSpy).not.toBeCalled(); + expect(mockUserFindByEmail).toHaveBeenCalledTimes(1); + expect(bcryptHashSpy).not.toHaveBeenCalled(); + expect(mockUserCreate).not.toHaveBeenCalled(); + expect(createTokensSpy).not.toHaveBeenCalled(); }); it('Should send success response for correct data', async () => { @@ -168,11 +168,11 @@ describe('Signup basic route', () => { expect(response.body.data.tokens).toHaveProperty('accessToken'); expect(response.body.data.tokens).toHaveProperty('refreshToken'); - expect(mockUserFindByEmail).toBeCalledTimes(1); - expect(bcryptHashSpy).toBeCalledTimes(1); - expect(mockUserCreate).toBeCalledTimes(1); - expect(createTokensSpy).toBeCalledTimes(1); + expect(mockUserFindByEmail).toHaveBeenCalledTimes(1); + expect(bcryptHashSpy).toHaveBeenCalledTimes(1); + expect(mockUserCreate).toHaveBeenCalledTimes(1); + expect(createTokensSpy).toHaveBeenCalledTimes(1); - expect(bcryptHashSpy).toBeCalledWith(USER_PASSWORD, 10); + expect(bcryptHashSpy).toHaveBeenCalledWith(USER_PASSWORD, 10); }); }); diff --git a/tests/routes/blog/index/unit.test.ts b/tests/routes/blog/index/unit.test.ts index c153529..63f75ad 100644 --- a/tests/routes/blog/index/unit.test.ts +++ b/tests/routes/blog/index/unit.test.ts @@ -33,7 +33,7 @@ describe('BlogDetail by URL route', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/endpoint/); expect(response.body.message).toMatch(/required/); - expect(mockPublishedBlogFindByUrl).not.toBeCalled(); + expect(mockPublishedBlogFindByUrl).not.toHaveBeenCalled(); }); it('Should send error when url endpoint is more that 200 chars', async () => { @@ -44,7 +44,7 @@ describe('BlogDetail by URL route', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/length must/); expect(response.body.message).toMatch(/200/); - expect(mockPublishedBlogFindByUrl).not.toBeCalled(); + expect(mockPublishedBlogFindByUrl).not.toHaveBeenCalled(); }); it('Should send error when blog do not exists for url', async () => { @@ -53,11 +53,11 @@ describe('BlogDetail by URL route', () => { ); expect(response.status).toBe(404); expect(response.body.message).toMatch(/not found/); - expect(mockBlogCacheFetchByUrl).toBeCalledTimes(1); - expect(mockBlogCacheFetchByUrl).toBeCalledWith('xyz'); - expect(mockBlogCacheSave).not.toBeCalled(); - expect(mockPublishedBlogFindByUrl).toBeCalledTimes(1); - expect(mockPublishedBlogFindByUrl).toBeCalledWith('xyz'); + expect(mockBlogCacheFetchByUrl).toHaveBeenCalledTimes(1); + expect(mockBlogCacheFetchByUrl).toHaveBeenCalledWith('xyz'); + expect(mockBlogCacheSave).not.toHaveBeenCalled(); + expect(mockPublishedBlogFindByUrl).toHaveBeenCalledTimes(1); + expect(mockPublishedBlogFindByUrl).toHaveBeenCalledWith('xyz'); }); it('Should send cache data when blog exists for url in cache', async () => { @@ -69,12 +69,12 @@ describe('BlogDetail by URL route', () => { expect(response.body.data).toBeDefined(); expect(response.body.data).toHaveProperty('_id'); - expect(mockBlogCacheFetchByUrl).toBeCalledTimes(1); - expect(mockBlogCacheFetchByUrl).toBeCalledWith(BLOG_URL); + expect(mockBlogCacheFetchByUrl).toHaveBeenCalledTimes(1); + expect(mockBlogCacheFetchByUrl).toHaveBeenCalledWith(BLOG_URL); expect(mockBlogCacheFetchByUrl).toReturnTimes(1); - expect(mockPublishedBlogFindByUrl).not.toBeCalled(); - expect(mockBlogCacheSave).not.toBeCalled(); + expect(mockPublishedBlogFindByUrl).not.toHaveBeenCalled(); + expect(mockBlogCacheSave).not.toHaveBeenCalled(); }); it('Should send database data when blog dont exists for url in cache', async () => { @@ -86,15 +86,15 @@ describe('BlogDetail by URL route', () => { expect(response.body.data).toBeDefined(); expect(response.body.data).toHaveProperty('_id'); - expect(mockBlogCacheFetchByUrl).toBeCalledTimes(1); - expect(mockBlogCacheFetchByUrl).toBeCalledWith(BLOG_2_URL); + expect(mockBlogCacheFetchByUrl).toHaveBeenCalledTimes(1); + expect(mockBlogCacheFetchByUrl).toHaveBeenCalledWith(BLOG_2_URL); expect(mockBlogCacheFetchByUrl).toReturnTimes(1); - expect(mockPublishedBlogFindByUrl).toBeCalledTimes(1); - expect(mockPublishedBlogFindByUrl).toBeCalledWith(BLOG_2_URL); + expect(mockPublishedBlogFindByUrl).toHaveBeenCalledTimes(1); + expect(mockPublishedBlogFindByUrl).toHaveBeenCalledWith(BLOG_2_URL); expect(mockPublishedBlogFindByUrl).toReturnTimes(1); - expect(mockBlogCacheSave).toBeCalledTimes(1); + expect(mockBlogCacheSave).toHaveBeenCalledTimes(1); expect(mockBlogCacheSave).toReturnTimes(1); }); }); @@ -113,7 +113,7 @@ describe('BlogDetail by id route', () => { const response = await addHeaders(request.get(endpoint + 'abc')); expect(response.status).toBe(400); expect(response.body.message).toMatch(/invalid/); - expect(mockPublishedBlogFindById).not.toBeCalled(); + expect(mockPublishedBlogFindById).not.toHaveBeenCalled(); }); it('Should send error when blog do not exists for id', async () => { @@ -122,7 +122,7 @@ describe('BlogDetail by id route', () => { ); expect(response.status).toBe(404); expect(response.body.message).toMatch(/not found/); - expect(mockPublishedBlogFindById).toBeCalledTimes(1); + expect(mockPublishedBlogFindById).toHaveBeenCalledTimes(1); }); it('Should send cache data when blog exists for id in cache', async () => { @@ -134,12 +134,12 @@ describe('BlogDetail by id route', () => { expect(response.body.data).toBeDefined(); expect(response.body.data).toHaveProperty('_id'); - expect(mockBlogCacheFetchById).toBeCalledTimes(1); - expect(mockBlogCacheFetchById).toBeCalledWith(BLOG_ID); + expect(mockBlogCacheFetchById).toHaveBeenCalledTimes(1); + expect(mockBlogCacheFetchById).toHaveBeenCalledWith(BLOG_ID); expect(mockBlogCacheFetchById).toReturnTimes(1); - expect(mockPublishedBlogFindById).not.toBeCalled(); - expect(mockBlogCacheSave).not.toBeCalled(); + expect(mockPublishedBlogFindById).not.toHaveBeenCalled(); + expect(mockBlogCacheSave).not.toHaveBeenCalled(); }); it('Should send database data when blog dont exists for url in cache', async () => { @@ -151,15 +151,15 @@ describe('BlogDetail by id route', () => { expect(response.body.data).toBeDefined(); expect(response.body.data).toHaveProperty('_id'); - expect(mockBlogCacheFetchById).toBeCalledTimes(1); - expect(mockBlogCacheFetchById).toBeCalledWith(BLOG_2_ID); + expect(mockBlogCacheFetchById).toHaveBeenCalledTimes(1); + expect(mockBlogCacheFetchById).toHaveBeenCalledWith(BLOG_2_ID); expect(mockBlogCacheFetchById).toReturnTimes(1); - expect(mockPublishedBlogFindById).toBeCalledTimes(1); - expect(mockPublishedBlogFindById).toBeCalledWith(BLOG_2_ID); + expect(mockPublishedBlogFindById).toHaveBeenCalledTimes(1); + expect(mockPublishedBlogFindById).toHaveBeenCalledWith(BLOG_2_ID); expect(mockPublishedBlogFindById).toReturnTimes(1); - expect(mockBlogCacheSave).toBeCalledTimes(1); + expect(mockBlogCacheSave).toHaveBeenCalledTimes(1); expect(mockBlogCacheSave).toReturnTimes(1); }); }); diff --git a/tests/routes/blog/writer/unit.test.ts b/tests/routes/blog/writer/unit.test.ts index 43c991c..40d3bb1 100644 --- a/tests/routes/blog/writer/unit.test.ts +++ b/tests/routes/blog/writer/unit.test.ts @@ -32,8 +32,8 @@ describe('Writer blog create routes', () => { const response = await addAuthHeaders(request.post(endpoint)); expect(response.status).toBe(401); expect(response.body.message).toMatch(/permission denied/i); - expect(mockBlogFindUrlIfExists).not.toBeCalled(); - expect(mockBlogCreate).not.toBeCalled(); + expect(mockBlogFindUrlIfExists).not.toHaveBeenCalled(); + expect(mockBlogCreate).not.toHaveBeenCalled(); }); it('Should send error if blog title not sent', async () => { @@ -48,8 +48,8 @@ describe('Writer blog create routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/title/i); expect(response.body.message).toMatch(/required/i); - expect(mockBlogFindUrlIfExists).not.toBeCalled(); - expect(mockBlogCreate).not.toBeCalled(); + expect(mockBlogFindUrlIfExists).not.toHaveBeenCalled(); + expect(mockBlogCreate).not.toHaveBeenCalled(); }); it('Should send error if blog description not sent', async () => { @@ -64,8 +64,8 @@ describe('Writer blog create routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/description/i); expect(response.body.message).toMatch(/required/i); - expect(mockBlogFindUrlIfExists).not.toBeCalled(); - expect(mockBlogCreate).not.toBeCalled(); + expect(mockBlogFindUrlIfExists).not.toHaveBeenCalled(); + expect(mockBlogCreate).not.toHaveBeenCalled(); }); it('Should send error if blog text not sent', async () => { @@ -80,8 +80,8 @@ describe('Writer blog create routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/text/i); expect(response.body.message).toMatch(/required/i); - expect(mockBlogFindUrlIfExists).not.toBeCalled(); - expect(mockBlogCreate).not.toBeCalled(); + expect(mockBlogFindUrlIfExists).not.toHaveBeenCalled(); + expect(mockBlogCreate).not.toHaveBeenCalled(); }); it('Should send error if blog blogUrl not sent', async () => { @@ -96,8 +96,8 @@ describe('Writer blog create routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/blogUrl/i); expect(response.body.message).toMatch(/required/i); - expect(mockBlogFindUrlIfExists).not.toBeCalled(); - expect(mockBlogCreate).not.toBeCalled(); + expect(mockBlogFindUrlIfExists).not.toHaveBeenCalled(); + expect(mockBlogCreate).not.toHaveBeenCalled(); }); it('Should send error if blog blogUrl is not in accepted format', async () => { @@ -113,8 +113,8 @@ describe('Writer blog create routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/blogUrl/i); expect(response.body.message).toMatch(/invalid/i); - expect(mockBlogFindUrlIfExists).not.toBeCalled(); - expect(mockBlogCreate).not.toBeCalled(); + expect(mockBlogFindUrlIfExists).not.toHaveBeenCalled(); + expect(mockBlogCreate).not.toHaveBeenCalled(); }); it('Should send error if blog imgUrl is not an url', async () => { @@ -131,8 +131,8 @@ describe('Writer blog create routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/imgUrl/i); expect(response.body.message).toMatch(/valid uri/i); - expect(mockBlogFindUrlIfExists).not.toBeCalled(); - expect(mockBlogCreate).not.toBeCalled(); + expect(mockBlogFindUrlIfExists).not.toHaveBeenCalled(); + expect(mockBlogCreate).not.toHaveBeenCalled(); }); it('Should send error if blog score is invalid', async () => { @@ -148,8 +148,8 @@ describe('Writer blog create routes', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/must be a number/i); - expect(mockBlogFindUrlIfExists).not.toBeCalled(); - expect(mockBlogCreate).not.toBeCalled(); + expect(mockBlogFindUrlIfExists).not.toHaveBeenCalled(); + expect(mockBlogCreate).not.toHaveBeenCalled(); }); it('Should send error if blog tags is invalid', async () => { @@ -166,8 +166,8 @@ describe('Writer blog create routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/must be/i); expect(response.body.message).toMatch(/array/i); - expect(mockBlogFindUrlIfExists).not.toBeCalled(); - expect(mockBlogCreate).not.toBeCalled(); + expect(mockBlogFindUrlIfExists).not.toHaveBeenCalled(); + expect(mockBlogCreate).not.toHaveBeenCalled(); }); it('Should send error if blog already exists for blogUrl', async () => { @@ -182,8 +182,8 @@ describe('Writer blog create routes', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/already exists/i); - expect(mockBlogFindUrlIfExists).toBeCalledTimes(1); - expect(mockBlogCreate).not.toBeCalled(); + expect(mockBlogFindUrlIfExists).toHaveBeenCalledTimes(1); + expect(mockBlogCreate).not.toHaveBeenCalled(); }); it('Should send success if blog data is correct', async () => { @@ -201,8 +201,8 @@ describe('Writer blog create routes', () => { ); expect(response.status).toBe(200); expect(response.body.message).toMatch(/created success/i); - expect(mockBlogFindUrlIfExists).toBeCalledTimes(1); - expect(mockBlogCreate).toBeCalledTimes(1); + expect(mockBlogFindUrlIfExists).toHaveBeenCalledTimes(1); + expect(mockBlogCreate).toHaveBeenCalledTimes(1); expect(response.body.data).toMatchObject({ _id: BLOG_ID.toHexString() }); }); }); @@ -224,8 +224,8 @@ describe('Writer blog submit routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/id/i); expect(response.body.message).toMatch(/invalid/i); - expect(mockFindBlogAllDataById).not.toBeCalled(); - expect(mockBlogUpdate).not.toBeCalled(); + expect(mockFindBlogAllDataById).not.toHaveBeenCalled(); + expect(mockBlogUpdate).not.toHaveBeenCalled(); }); it('Should send error if submit blog do not exist for id', async () => { @@ -235,8 +235,8 @@ describe('Writer blog submit routes', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/not exists/i); - expect(mockFindBlogAllDataById).toBeCalledTimes(1); - expect(mockBlogUpdate).not.toBeCalled(); + expect(mockFindBlogAllDataById).toHaveBeenCalledTimes(1); + expect(mockBlogUpdate).not.toHaveBeenCalled(); }); it('Should send success if submit blog for id exists', async () => { @@ -246,8 +246,8 @@ describe('Writer blog submit routes', () => { ); expect(response.status).toBe(200); expect(response.body.message).toMatch(/submitted success/i); - expect(mockFindBlogAllDataById).toBeCalledTimes(1); - expect(mockBlogUpdate).toBeCalledTimes(1); + expect(mockFindBlogAllDataById).toHaveBeenCalledTimes(1); + expect(mockBlogUpdate).toHaveBeenCalledTimes(1); }); }); @@ -268,8 +268,8 @@ describe('Writer blog withdraw routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/id/i); expect(response.body.message).toMatch(/invalid/i); - expect(mockFindBlogAllDataById).not.toBeCalled(); - expect(mockBlogUpdate).not.toBeCalled(); + expect(mockFindBlogAllDataById).not.toHaveBeenCalled(); + expect(mockBlogUpdate).not.toHaveBeenCalled(); }); it('Should send error if withdraw blog do not exist for id', async () => { @@ -279,8 +279,8 @@ describe('Writer blog withdraw routes', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/not exists/i); - expect(mockFindBlogAllDataById).toBeCalledTimes(1); - expect(mockBlogUpdate).not.toBeCalled(); + expect(mockFindBlogAllDataById).toHaveBeenCalledTimes(1); + expect(mockBlogUpdate).not.toHaveBeenCalled(); }); it('Should send success if withdraw blog for id exists', async () => { @@ -290,8 +290,8 @@ describe('Writer blog withdraw routes', () => { ); expect(response.status).toBe(200); expect(response.body.message).toMatch(/withdrawn success/i); - expect(mockFindBlogAllDataById).toBeCalledTimes(1); - expect(mockBlogUpdate).toBeCalledTimes(1); + expect(mockFindBlogAllDataById).toHaveBeenCalledTimes(1); + expect(mockBlogUpdate).toHaveBeenCalledTimes(1); }); }); @@ -312,8 +312,8 @@ describe('Writer blog delete routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/id/i); expect(response.body.message).toMatch(/invalid/i); - expect(mockFindBlogAllDataById).not.toBeCalled(); - expect(mockBlogUpdate).not.toBeCalled(); + expect(mockFindBlogAllDataById).not.toHaveBeenCalled(); + expect(mockBlogUpdate).not.toHaveBeenCalled(); }); it('Should send error if deleting blog do not exist for id', async () => { @@ -323,8 +323,8 @@ describe('Writer blog delete routes', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/not exists/i); - expect(mockFindBlogAllDataById).toBeCalledTimes(1); - expect(mockBlogUpdate).not.toBeCalled(); + expect(mockFindBlogAllDataById).toHaveBeenCalledTimes(1); + expect(mockBlogUpdate).not.toHaveBeenCalled(); }); it('Should send success if deleting blog for id exists', async () => { @@ -334,8 +334,8 @@ describe('Writer blog delete routes', () => { ); expect(response.status).toBe(200); expect(response.body.message).toMatch(/deleted success/i); - expect(mockFindBlogAllDataById).toBeCalledTimes(1); - expect(mockBlogUpdate).toBeCalledTimes(1); + expect(mockFindBlogAllDataById).toHaveBeenCalledTimes(1); + expect(mockBlogUpdate).toHaveBeenCalledTimes(1); }); }); @@ -355,7 +355,7 @@ describe('Writer blog get by id routes', () => { expect(response.status).toBe(400); expect(response.body.message).toMatch(/id/i); expect(response.body.message).toMatch(/invalid/i); - expect(mockFindBlogAllDataById).not.toBeCalled(); + expect(mockFindBlogAllDataById).not.toHaveBeenCalled(); }); it('Should send error if fetching blog do not exist for id', async () => { @@ -365,7 +365,7 @@ describe('Writer blog get by id routes', () => { ); expect(response.status).toBe(400); expect(response.body.message).toMatch(/not exists/i); - expect(mockFindBlogAllDataById).toBeCalledTimes(1); + expect(mockFindBlogAllDataById).toHaveBeenCalledTimes(1); }); it('Should send error if author is different', async () => { @@ -376,7 +376,7 @@ describe('Writer blog get by id routes', () => { expect(response.status).toBe(403); expect(response.body.message).toMatch(/don't have/i); expect(response.body.message).toMatch(/permission/i); - expect(mockFindBlogAllDataById).toBeCalledTimes(1); + expect(mockFindBlogAllDataById).toHaveBeenCalledTimes(1); }); it('Should send success if fetching blog for id exists', async () => { @@ -386,7 +386,7 @@ describe('Writer blog get by id routes', () => { ); expect(response.status).toBe(200); expect(response.body.message).toMatch(/success/i); - expect(mockFindBlogAllDataById).toBeCalledTimes(1); + expect(mockFindBlogAllDataById).toHaveBeenCalledTimes(1); expect(response.body.data).toMatchObject({ _id: BLOG_ID.toHexString() }); }); });