-
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
Showing
2 changed files
with
59 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Saga, SagaAction} from "../Hyundux-saga/Saga" | ||
import { Story } from "../Hyundux-saga/Story"; | ||
import { HyunduxTest } from "../Hyundux-test/Tester"; | ||
import State, { createState } from "../Hyundux/State"; | ||
|
||
type MockStory = (request: object) => Promise<object>; | ||
|
||
|
||
export class SagaTest<T> { | ||
saga: Saga; | ||
currentState: State<T> | ||
currentAction: SagaAction | ||
currentStory: Story | ||
currentParameter: object | ||
constructor(tester: HyunduxTest<T>) { | ||
this.saga = new Saga(tester.store) | ||
this.currentState = this.saga.store.states[0] as State<T> | ||
} | ||
|
||
given(givenPayload: T) { | ||
if (this.saga !== null) { | ||
this.currentState = createState(this.currentState.type, givenPayload); | ||
} | ||
return this; | ||
} | ||
|
||
when(action: SagaAction, story: Story, parameter: object) { | ||
this.currentAction = action | ||
this.currentStory = story | ||
this.currentParameter = parameter | ||
|
||
return this | ||
} | ||
|
||
async then(expectedPayload: T, isTrue: boolean = true){ | ||
await this.saga.run(this.currentAction, this.currentStory, this.currentParameter) | ||
const result = this.saga.store.states[0] | ||
if(isTrue){ | ||
expect(result.payload).toEqual(expectedPayload); | ||
} | ||
else { | ||
expect(result.payload).not.toEqual(expectedPayload); | ||
} | ||
} | ||
|
||
sleep(ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} | ||
} | ||
|
||
export const createSagaTester = <T,>(tester :HyunduxTest<T>) => { | ||
return new SagaTest(tester) | ||
} |