Repository navigation
fix(core): throw the intended error when no reducer matches an event #766
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
Open
zigzagdev
wants to merge
2
commits into
daangn:main
Choose a base branch
from
zigzagdev:fix/make-reducer-error-dead-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,58 @@ | ||
| import { makeEvent } from "../event-utils"; | ||
| import type { Activity } from "../Stack"; | ||
| import { makeActivityReducer } from "./makeActivityReducer"; | ||
|
|
||
| function makeActivity(): Activity { | ||
| const pushedEvent = makeEvent("Pushed", { | ||
| activityId: "a1", | ||
| activityName: "sample", | ||
| activityParams: { page: "1" }, | ||
| }); | ||
|
|
||
| const stepPushedEvent = makeEvent("StepPushed", { | ||
| stepId: "s2", | ||
| stepParams: { page: "2" }, | ||
| }); | ||
|
|
||
| return { | ||
| id: "a1", | ||
| name: "sample", | ||
| transitionState: "enter-done", | ||
| params: { page: "2" }, | ||
| enteredBy: pushedEvent, | ||
| steps: [ | ||
| { | ||
| id: "a1", | ||
| params: { page: "1" }, | ||
| enteredBy: pushedEvent, | ||
| zIndex: -1, | ||
| }, | ||
| { | ||
| id: "s2", | ||
| params: { page: "2" }, | ||
| enteredBy: stepPushedEvent, | ||
| zIndex: -1, | ||
| }, | ||
| ], | ||
| isTop: true, | ||
| isActive: true, | ||
| isRoot: true, | ||
| zIndex: 0, | ||
| }; | ||
| } | ||
|
|
||
| test("makeActivityReducer - StepPopped는 activity.steps를 변경하지 않고 새 배열을 반환합니다", () => { | ||
| const activity = makeActivity(); | ||
| const originalSteps = activity.steps; | ||
|
|
||
| const reducer = makeActivityReducer({ transitionDuration: 300, now: 0 }); | ||
| const stepPoppedEvent = makeEvent("StepPopped", {}); | ||
|
|
||
| const nextActivity = reducer(activity, stepPoppedEvent); | ||
|
|
||
| expect(activity.steps).toBe(originalSteps); | ||
| expect(activity.steps.length).toBe(2); | ||
| expect(nextActivity.steps).not.toBe(originalSteps); | ||
| expect(nextActivity.steps.length).toBe(1); | ||
| expect(nextActivity.params).toEqual({ page: "1" }); | ||
| }); |
This file contains hidden or 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 hidden or 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,50 @@ | ||
| import type { DomainEvent } from "../event-types"; | ||
| import { makeReducer } from "./makeReducer"; | ||
|
|
||
| test("makeReducer - 등록된 이벤트는 해당 reducer로 처리됩니다", () => { | ||
| const reducer = makeReducer<number>({ | ||
| Initialized: (state) => state + 1, | ||
| ActivityRegistered: (state) => state, | ||
| Pushed: (state) => state, | ||
| Replaced: (state) => state, | ||
| Popped: (state) => state, | ||
| StepPushed: (state) => state, | ||
| StepReplaced: (state) => state, | ||
| StepPopped: (state) => state, | ||
| Paused: (state) => state, | ||
| Resumed: (state) => state, | ||
| }); | ||
|
|
||
| const event = { | ||
| id: "e1", | ||
| name: "Initialized", | ||
| eventDate: 0, | ||
| } as unknown as DomainEvent; | ||
|
|
||
| expect(reducer(0, event)).toBe(1); | ||
| }); | ||
|
|
||
| test("makeReducer - 등록되지 않은 이벤트는 명확한 에러를 던집니다", () => { | ||
| const reducer = makeReducer<number>({ | ||
| Initialized: (state) => state, | ||
| ActivityRegistered: (state) => state, | ||
| Pushed: (state) => state, | ||
| Replaced: (state) => state, | ||
| Popped: (state) => state, | ||
| StepPushed: (state) => state, | ||
| StepReplaced: (state) => state, | ||
| StepPopped: (state) => state, | ||
| Paused: (state) => state, | ||
| Resumed: (state) => state, | ||
| }); | ||
|
|
||
| const unknownEvent = { | ||
| id: "e1", | ||
| name: "Unknown", | ||
| eventDate: 0, | ||
| } as unknown as DomainEvent; | ||
|
|
||
| expect(() => reducer(0, unknownEvent)).toThrow( | ||
| `No reducer for event ${JSON.stringify(unknownEvent)}`, | ||
| ); | ||
| }); |
This file contains hidden or 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
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Check for an own reducer before dispatch.
If an unregistered event has
name: "toString", this lookup selects an inherited function. The reducer then returns a string instead of throwingNo reducer for event. CheckObject.prototype.hasOwnProperty.call(reducerMap, event.name)before binding, and cover an inherited name in the tests.🤖 Prompt for AI Agents