-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from composable-com/feat/testing-2
Additional unit tests
- Loading branch information
Showing
24 changed files
with
560 additions
and
135 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
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
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,9 @@ | ||
const mockEnvironmentVariables = () => { | ||
process.env.CTP_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxx'; | ||
process.env.CTP_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | ||
process.env.CTP_PROJECT_KEY = 'xxxxxxxxxxx'; | ||
process.env.CTP_SCOPE = 'xxxxxxxxxxx'; | ||
process.env.CTP_REGION = 'us-central1.gcp'; | ||
}; | ||
|
||
mockEnvironmentVariables(); |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,36 @@ | ||
import { afterEach, describe, expect, it, jest } from '@jest/globals'; | ||
import { createType, createMyExtension } from './actions' | ||
import { run } from './post-deploy' | ||
|
||
jest.mock('./actions') | ||
|
||
describe('Post-deploy', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks() | ||
jest.restoreAllMocks() | ||
jest.resetModules() | ||
}) | ||
|
||
it('should run start function', async () => { | ||
(createMyExtension as jest.Mock).mockReturnValue({ | ||
data: 'success', | ||
}); | ||
|
||
await run() | ||
expect(createMyExtension).toHaveBeenCalled() | ||
expect(createType).toHaveBeenCalled() | ||
}) | ||
|
||
it('should handle errors', async () => { | ||
const errorMessage = 'Something went wrong'; | ||
(createMyExtension as jest.Mock).mockImplementation(() => { | ||
throw new Error(errorMessage); | ||
}); | ||
|
||
const stderrSpy = jest.spyOn(process.stderr, 'write'); | ||
await run(); | ||
|
||
expect(stderrSpy).toHaveBeenCalledWith(`Post-deploy failed: Error: ${errorMessage}`); | ||
expect(process.exitCode).toBe(1); | ||
}); | ||
}) |
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
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,38 @@ | ||
import { afterEach, describe, expect, it, jest } from '@jest/globals'; | ||
import { deleteMyExtension } from './actions'; | ||
import { run } from './pre-undeploy'; | ||
import { createApiRoot } from '../services/commercetools/client/create.client' | ||
|
||
jest.mock('./actions') | ||
jest.mock('../services/commercetools/client/create.client') | ||
|
||
describe('Pre-undeploy', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks() | ||
jest.restoreAllMocks() | ||
jest.resetModules() | ||
}) | ||
|
||
it('should run start function', async () => { | ||
(deleteMyExtension as jest.Mock).mockReturnValue({ | ||
data: 'success', | ||
}); | ||
|
||
await run() | ||
expect(createApiRoot).toHaveBeenCalled() | ||
expect(deleteMyExtension).toHaveBeenCalled() | ||
}) | ||
|
||
it('should handle errors', async () => { | ||
const errorMessage = 'Something went wrong'; | ||
(deleteMyExtension as jest.Mock).mockImplementation(() => { | ||
throw new Error(errorMessage); | ||
}); | ||
|
||
const stderrSpy = jest.spyOn(process.stderr, 'write'); | ||
await run(); | ||
|
||
expect(stderrSpy).toHaveBeenCalledWith(`Pre-undeploy failed: Error: ${errorMessage}`); | ||
expect(process.exitCode).toBe(1); | ||
}); | ||
}) |
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
45 changes: 45 additions & 0 deletions
45
talonone-service/src/handlers/actions/cart-actions.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,45 @@ | ||
import { Effect } from 'talon_one' | ||
import { Cart } from '@commercetools/platform-sdk' | ||
import { EffectHandlers } from '../effects/types' | ||
import { getCartActions } from './cart-actions' | ||
|
||
describe('getCartActions', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
jest.restoreAllMocks() | ||
}) | ||
it('should return empty array if no effects are given', () => { | ||
const mockCart = {} as unknown as Cart | ||
const mockEffects = [] as unknown as Effect[] | ||
const mockHandlers = { | ||
someEffectType: jest.fn().mockReturnValue('someActionResult') | ||
} | ||
|
||
const result = getCartActions(mockCart, mockEffects, mockHandlers) | ||
expect(result).toEqual([]) | ||
}) | ||
|
||
it('should return the correct cart actions based on given effects and handlers', () => { | ||
const mockCart = {} as unknown as Cart | ||
|
||
const mockEffects = [ | ||
{ effectType: 'someEffectType', props: { key: 'value' } }, | ||
{ effectType: 'unhandledEffectType', props: { key2: 'value2' } } | ||
] as unknown as Effect[] | ||
|
||
const mockHandlers = { | ||
someEffectType: jest.fn().mockReturnValue('someActionResult') | ||
} | ||
|
||
const result = getCartActions(mockCart, mockEffects, mockHandlers) | ||
|
||
// Assert that the handler for 'someEffectType' was called with correct properties | ||
expect(mockHandlers.someEffectType).toHaveBeenCalledWith({ | ||
key: 'value', | ||
cart: mockCart | ||
}) | ||
|
||
// Assert the expected result | ||
expect(result).toEqual(['someActionResult']) | ||
}) | ||
}) |
57 changes: 57 additions & 0 deletions
57
talonone-service/src/handlers/actions/order-actions.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,57 @@ | ||
import { Effect } from 'talon_one' | ||
import { Order } from '@commercetools/platform-sdk' | ||
import { getOrderActions } from './order-actions' | ||
|
||
describe('getCartActions', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
it('should return an empty array if no effects are provided', async () => { | ||
const mockOrder = { | ||
anonymousId: null | ||
} as unknown as Order | ||
|
||
const result = await getOrderActions(mockOrder, [], {}) | ||
expect(result).toEqual([]) | ||
}) | ||
|
||
it('should return an empty array if order has an anonymousId', async () => { | ||
const mockOrder = { | ||
anonymousId: 'someId' | ||
} as unknown as Order | ||
|
||
const mockEffects = [ | ||
{ effectType: 'someEffectType', props: { key: 'value' } } | ||
] as unknown as Effect[] | ||
|
||
const result = await getOrderActions(mockOrder, mockEffects, {}) | ||
expect(result).toEqual([]) | ||
}) | ||
|
||
it('should return the correct order actions based on given effects and handlers', async () => { | ||
const mockOrder = { | ||
anonymousId: null | ||
} as unknown as Order | ||
|
||
const mockEffects = [ | ||
{ effectType: 'someEffectType', props: { key: 'value' } }, | ||
{ effectType: 'unhandledEffectType', props: { key2: 'value2' } } | ||
] as unknown as Effect[] | ||
|
||
const mockHandlerResult = 'someActionResult' | ||
const mockHandlers = { | ||
someEffectType: jest.fn().mockResolvedValue(mockHandlerResult) | ||
} | ||
|
||
const result = await getOrderActions(mockOrder, mockEffects, mockHandlers) | ||
|
||
expect(mockHandlers.someEffectType).toHaveBeenCalledWith({ | ||
key: 'value', | ||
order: mockOrder | ||
}) | ||
|
||
expect(result).toEqual([mockHandlerResult]) | ||
}) | ||
}) |
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
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,27 @@ | ||
import { describe, expect, jest } from '@jest/globals'; | ||
import { EffectHandlers } from '../types'; | ||
import addLoyaltyPointsHandler from './addLoyaltyPoints'; | ||
import getOrderEffectHandlers from './index'; | ||
import rollbackAddedLoyaltyPointsHandler from './rollbackAddedLoyaltyPoints'; | ||
|
||
jest.mock('./addLoyaltyPoints') | ||
jest.mock('./rollbackAddedLoyaltyPoints') | ||
|
||
describe('getOrderEffectHandlers', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should return an object with the addLoyaltyPoints and rollbackAddedLoyaltyPoints effect handlers', () => { | ||
(addLoyaltyPointsHandler as jest.Mock).mockReturnValue(null); | ||
(rollbackAddedLoyaltyPointsHandler as jest.Mock).mockReturnValue(null); | ||
|
||
const effectHandlers: EffectHandlers = getOrderEffectHandlers() | ||
|
||
expect(effectHandlers).toHaveProperty('addLoyaltyPoints') | ||
expect(effectHandlers).toHaveProperty('rollbackAddedLoyaltyPoints') | ||
expect(effectHandlers.addLoyaltyPoints({})).toBeNull() | ||
expect(effectHandlers.rollbackAddedLoyaltyPoints({})).toBeNull() | ||
}) | ||
}) |
Oops, something went wrong.