Skip to content

Commit cb934b0

Browse files
committed
Add more tests
1 parent a06401e commit cb934b0

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

src/commons/redux/__tests__/utils.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { testSaga } from "redux-saga-test-plan"
22
import WorkspaceActions from "src/commons/workspace/WorkspaceActions"
33

4-
import { combineSagaHandlers } from "../utils"
4+
import { combineSagaHandlers, createActions } from "../utils"
55

66
// Would have used spyOn, but for some reason that doesn't work properly
77
jest.mock('src/commons/sagas/SafeEffects', () => ({
@@ -26,6 +26,10 @@ test('test combineSagaHandlers', () => {
2626
},
2727
[WorkspaceActions.toggleUsingSubst.type]: {
2828
takeLeading: mockTakeLeadingHandler
29+
},
30+
[WorkspaceActions.toggleEditorAutorun.type]: {
31+
takeEvery: mockTakeEveryHandler,
32+
takeLeading: mockTakeLeadingHandler
2933
}
3034
})
3135

@@ -39,5 +43,27 @@ test('test combineSagaHandlers', () => {
3943
.next()
4044
.takeLeading(WorkspaceActions.toggleUsingSubst.type, mockTakeLeadingHandler)
4145
.next()
46+
.takeEvery(WorkspaceActions.toggleEditorAutorun.type, mockTakeEveryHandler)
47+
.next()
48+
.takeLeading(WorkspaceActions.toggleEditorAutorun.type, mockTakeLeadingHandler)
49+
.next()
4250
.isDone()
4351
})
52+
53+
test('createActions', () => {
54+
const actions = createActions('workspace', {
55+
act0: false,
56+
act1: (value: string) => ({ value }),
57+
act2: 525600
58+
})
59+
60+
const act0 = actions.act0()
61+
expect(act0.type).toEqual('workspace/act0')
62+
63+
const act1 = actions.act1('test')
64+
expect(act1.type).toEqual('workspace/act1')
65+
expect(act1.payload).toMatchObject({ value: 'test' })
66+
67+
const act2 = actions.act2()
68+
expect(act2.type).toEqual('workspace/act2')
69+
})

src/commons/redux/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { type ActionTypeToCreator, objectEntries } from '../utils/TypeHelper';
1414
/**
1515
* Creates actions, given a base name and base actions
1616
* @param baseName The base name of the actions
17-
* @param baseActions The base actions. Use a falsy value to create an action without a payload.
17+
* @param baseActions The base actions. Use a non function value to create an action without a payload.
1818
* @returns An object containing the actions
1919
*/
2020
export function createActions<BaseName extends string, BaseActions extends Record<string, any>>(
@@ -24,7 +24,7 @@ export function createActions<BaseName extends string, BaseActions extends Recor
2424
return Object.entries(baseActions).reduce(
2525
(res, [name, func]) => ({
2626
...res,
27-
[name]: func
27+
[name]: typeof func === 'function'
2828
? createAction(`${baseName}/${name}`, (...args: any) => ({ payload: func(...args) }))
2929
: createAction(`${baseName}/${name}`)
3030
}),
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as Sentry from '@sentry/browser'
2+
import { call } from "redux-saga/effects"
3+
import { expectSaga } from "redux-saga-test-plan"
4+
5+
import { wrapSaga } from "../SafeEffects"
6+
7+
jest.spyOn(Sentry, 'captureException')
8+
9+
// Silence console error
10+
jest.spyOn(console, 'error').mockImplementation(x => {})
11+
12+
describe('Test wrapSaga', () => {
13+
test('wrapSaga is transparent', async () => {
14+
const mockFn = jest.fn()
15+
const wrappedSaga = wrapSaga(function* () {
16+
yield call(mockFn)
17+
})
18+
19+
await expectSaga(wrappedSaga).silentRun()
20+
21+
expect(mockFn).toHaveBeenCalledTimes(1)
22+
})
23+
24+
test('wrapSaga handles errors appropriately', async () => {
25+
const errorToThrow = new Error()
26+
const wrappedSaga = wrapSaga(function* () {
27+
throw errorToThrow
28+
})
29+
30+
await expectSaga(wrappedSaga).silentRun()
31+
32+
expect(Sentry.captureException).toHaveBeenCalledWith(errorToThrow)
33+
expect(console.error).toHaveBeenCalledTimes(1)
34+
})
35+
})

0 commit comments

Comments
 (0)