-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Refactor ActionWithPayload/ActionWithoutPayload to be uniform #605
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7342aed
fix: add failing ts test case
sseppola 7f155a5
fix: simplify Action types
sseppola d186d57
fix: tests and lint issues
sseppola 9b8a2e8
chore: upgrade rxjs dev dep
sseppola 65801d9
fix: use type import
sseppola c3fb96a
Update src/actionCreator.ts
sseppola a2ebf6f
fix: use type imports
sseppola 607bdfd
fix: outdated GH Action step
sseppola a5c4fb8
fix: remove CodeQL
sseppola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { Subject } from 'rxjs'; | ||
import { filter, map } from 'rxjs/operators'; | ||
import { marbles } from 'rxjs-marbles/jest'; | ||
import { ActionWithPayload } from './types/Action'; | ||
import { Action } from './types/Action'; | ||
import { | ||
Routine, | ||
collectRoutines, | ||
|
@@ -18,55 +18,67 @@ beforeAll(() => { | |
jest.useFakeTimers(); | ||
}); | ||
|
||
const actions = { | ||
a: mockAction('alpha', undefined, { letter: 'A' }), | ||
b: mockAction('bravo', undefined, { letter: 'B' }), | ||
c: mockAction('charlie', undefined, { letter: 'C' }), | ||
e: mockAction('error'), | ||
const marbleMap: Record<string, any> = { | ||
a: mockAction('[Mock] alpha', undefined, { letter: 'A' }), | ||
b: mockAction('[Mock] bravo', undefined, { letter: 'B' }), | ||
c: mockAction('[Mock] charlie', undefined, { letter: 'C' }), | ||
e: mockAction('[Mock] error'), | ||
f: mockAction('[Mock] action', undefined, { letter: 'F' }), | ||
}; | ||
const letters = { | ||
A: 'A', | ||
B: 'B', | ||
C: 'C', | ||
F: 'F', | ||
}; | ||
const lengths = { | ||
...letters, | ||
'5': 5, | ||
'7': 7, | ||
}; | ||
|
||
// Add letter representations to marble map | ||
for (let i = 'A'.charCodeAt(0); i < 'Z'.charCodeAt(0); i++) { | ||
const letter = String.fromCharCode(i); | ||
marbleMap[letter] = letter; | ||
} | ||
// Add number representations to marble map | ||
for (let i = 0; i < 10; i++) { | ||
// Create marble representations of the 10 first characters | ||
// eg. { A: 'A', B: 'B', ...etc } | ||
const charCodeA = 'A'.charCodeAt(0); | ||
const letter = String.fromCharCode(charCodeA + i); | ||
marbleMap[letter] = letter; | ||
// Create marble representations of the 10 first numbers from 0 | ||
marbleMap[`${i}`] = i; | ||
} | ||
Comment on lines
+29
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to change these tests because it was using the lengths of the action types.. which obviously changes when you enforce the |
||
|
||
const errors = { | ||
e: 'error', | ||
}; | ||
const actionMarbles1 = ' -a----b----c---'; | ||
const letterMarbles = ' -A----B----C---'; | ||
const combinedMarbles = '-(A5)-(B5)-(C7)'; | ||
const combinedMarbles = '-(A0)-(B1)-(C2)'; | ||
const actionMarbles2 = ' -a----e----a---'; | ||
const errorMarbles = ' ------e'; | ||
const errorSub1 = ' ^-----!'; | ||
const errorSub2 = ' ------^--------'; | ||
const singleActionMarble = ' -f---f'; | ||
|
||
const errorRoutine: Routine<string> = map((a) => { | ||
if (a.type === 'error') throw 'error'; | ||
if (a.type === '[Mock] error') throw 'error'; | ||
return 'passed'; | ||
}); | ||
const lettersRoutine = routine( | ||
filter( | ||
(action: any): action is ActionWithPayload<{ letter: string }> => | ||
action.payload !== undefined | ||
), | ||
filter((action: unknown): action is Action<{ letter: string }> => { | ||
const { payload } = action as Action<{ letter: string }>; | ||
return Boolean(payload.letter); | ||
}), | ||
extractPayload(), | ||
map(({ letter }) => letter) | ||
); | ||
const lengthRoutine = routine(map(({ type }) => type.length)); | ||
|
||
const letterToNumberRoutine = routine( | ||
map((action) => { | ||
const { payload } = action as Action<{ letter: string }>; | ||
return payload.letter.charCodeAt(0) - 'A'.charCodeAt(0); | ||
}) | ||
); | ||
|
||
test( | ||
'routine pipes multiple operator functions', | ||
marbles((m) => { | ||
const action$ = m.hot(actionMarbles1, actions); | ||
const expected$ = m.hot(letterMarbles, letters); | ||
const action$ = m.hot(actionMarbles1, marbleMap); | ||
const expected$ = m.hot(letterMarbles, marbleMap); | ||
|
||
const actual$ = action$.pipe(lettersRoutine); | ||
|
||
|
@@ -77,10 +89,10 @@ test( | |
test( | ||
'collectRoutines runs all routines', | ||
marbles((m) => { | ||
const action$ = m.hot(actionMarbles1, actions); | ||
const expected$ = m.hot(combinedMarbles, lengths); | ||
const action$ = m.hot(actionMarbles1, marbleMap); | ||
const expected$ = m.hot(combinedMarbles, marbleMap); | ||
const actual$ = action$.pipe( | ||
collectRoutines(lettersRoutine, lengthRoutine) | ||
collectRoutines(lettersRoutine, letterToNumberRoutine) | ||
); | ||
m.expect(actual$).toBeObservable(expected$); | ||
}) | ||
|
@@ -89,7 +101,7 @@ test( | |
test( | ||
'subscribeRoutine subscribes action$', | ||
marbles((m) => { | ||
const action$ = m.hot(actionMarbles1, actions); | ||
const action$ = m.hot(actionMarbles1, marbleMap); | ||
subscribeRoutine(action$, lettersRoutine); | ||
|
||
m.expect(action$).toHaveSubscriptions(['^']); | ||
|
@@ -99,7 +111,7 @@ test( | |
test( | ||
'subscribeRoutine resubscribes on errors', | ||
marbles((m) => { | ||
const action$ = m.hot(actionMarbles2, actions); | ||
const action$ = m.hot(actionMarbles2, marbleMap); | ||
|
||
subscribeRoutine(action$, errorRoutine); | ||
|
||
|
@@ -110,7 +122,7 @@ test( | |
test( | ||
'subscribeRoutine emits errors to error subject', | ||
marbles((m) => { | ||
const action$ = m.hot(actionMarbles2, actions); | ||
const action$ = m.hot(actionMarbles2, marbleMap); | ||
const error$ = new Subject<any>(); | ||
|
||
subscribeRoutine(action$, errorRoutine, error$); | ||
|
@@ -122,7 +134,7 @@ test( | |
test( | ||
'tapRoutine register a routine', | ||
marbles((m) => { | ||
const action$ = m.hot(singleActionMarble, actions); | ||
const action$ = m.hot(singleActionMarble, marbleMap); | ||
const action = actionCreator<{ letter: string }>('[Mock] action'); | ||
expect.assertions(2); | ||
const routineToSubscribe = tapRoutine(action, (payload) => | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case did not make sense.