-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5b1f52
commit e8e4848
Showing
6 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions
41
free-sample-product-service/src/utils/assert.utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { assert, assertError, assertString } from './assert.utils'; | ||
|
||
describe('assert', () => { | ||
it('should not throw an error when the condition is true', () => { | ||
expect(() => assert(true, 'Condition is true')).not.toThrow(); | ||
}); | ||
|
||
it('should throw an error when the condition is false', () => { | ||
expect(() => assert(false, 'Condition is false')).toThrow( | ||
'Assertion failed: Condition is false' | ||
); | ||
}); | ||
}); | ||
|
||
describe('assertError', () => { | ||
it('should not throw an error when the value is an Error', () => { | ||
const error = new Error('Test error'); | ||
expect(() => assertError(error, 'It is an Error')).not.toThrow(); | ||
}); | ||
|
||
it('should throw an error when the value is not an Error', () => { | ||
const nonErrorValue = 'Not an error'; | ||
expect(() => assertError(nonErrorValue, 'Not an Error')).toThrow( | ||
'Assertion failed: Not an Error' | ||
); | ||
}); | ||
}); | ||
|
||
describe('assertString', () => { | ||
it('should not throw an error when the value is a string', () => { | ||
const stringValue = 'Test string'; | ||
expect(() => assertString(stringValue, 'It is a string')).not.toThrow(); | ||
}); | ||
|
||
it('should throw an error when the value is not a string', () => { | ||
const nonStringValue = 10; | ||
expect(() => assertString(nonStringValue, 'It is a string')).toThrow( | ||
'Assertion failed: It is a string' | ||
); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
free-sample-product-service/src/utils/config.utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import CustomError from '../errors/custom.error'; | ||
import { readConfiguration } from './config.utils'; | ||
import * as validatorHelper from '../validators/helpers.validators'; | ||
|
||
// Mock .env | ||
const mockEnv = { | ||
CTP_CLIENT_ID: 'mockClientId', | ||
CTP_CLIENT_SECRET: 'mockClientSecret', | ||
CTP_PROJECT_KEY: 'mockProjectKey', | ||
CTP_REGION: 'mockRegion', | ||
SAMPLE_PRODUCT_QUANTITY: '100', | ||
SAMPLE_PRODUCT_SKU: 'mockProductSku', | ||
CHANNEL_KEY: 'mockChannelKey', | ||
SAMPLE_LINEITEM_KEY: 'mockLineItemKey', | ||
CART_MIN_VALUE: '10', | ||
}; | ||
|
||
describe('readConfiguration', () => { | ||
it('should return the correct configuration when env variables are valid', () => { | ||
process.env = mockEnv | ||
const expectedConfig = { | ||
clientId: 'mockClientId', | ||
clientSecret: 'mockClientSecret', | ||
projectKey: 'mockProjectKey', | ||
region: 'mockRegion', | ||
freeSampleQuantity: parseInt('100' || ''), | ||
freeSampleSku: 'mockProductSku', | ||
freeSampleChannelKey: 'mockChannelKey', | ||
freeLineItemKey: 'mockLineItemKey', | ||
minCartValue: parseInt('10' || ''), | ||
}; | ||
|
||
// Mock the validation function to return an empty array | ||
jest.spyOn(validatorHelper, 'getValidateMessages').mockReturnValue([]); | ||
|
||
const config = readConfiguration(); | ||
expect(config).toEqual(expectedConfig); | ||
}); | ||
|
||
it('should throw a CustomError when env variables are invalid', () => { | ||
process.env = mockEnv | ||
// Mock the validation function to return validation errors | ||
jest.spyOn(validatorHelper, 'getValidateMessages').mockReturnValue(['Invalid variable: CTP_CLIENT_ID']); | ||
|
||
expect(() => {readConfiguration();}).toThrow(CustomError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { logger } from "./logger.utils" | ||
|
||
describe("logger object", () => { | ||
it("should not throw an error when logger object", () => { | ||
expect(typeof logger).toEqual('object'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { describe, expect, test } from '@jest/globals'; | ||
|
||
describe('Testing Cart Controller', () => { | ||
test('POST `/service` route', async () => { | ||
test('POST `/free-sample-product-service` route', async () => { | ||
expect(1).toBe(1); | ||
}); | ||
}); |