Skip to content

Commit

Permalink
feat: saga test 구현 (CC-178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunkkkk committed Aug 23, 2024
1 parent ee61ef7 commit 403bf05
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
18 changes: 6 additions & 12 deletions Caecae/__tests__/FindGameTest/FindGameTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ test("결과보여지는 것들의 인덱스변경_입력값:2 -> 결과값 1 (
tester.given(givenData).when(whenData).then(thenData, false);
});

test("클릭 이벤트:입력값 [{x: 0.5 y: 0.5}, 틀린답 {x: 0.7, y: 0.1}] -> 결과값 answers.length == 1, answers = [answer{ x: 0.5. y: 0.5 }] (옳은 테스트)", () => {
test("클릭 이벤트:입력값 [{x: 0.5 y: 0.5}] -> 결과값 answers.length == 1, answers = [answer{ x: 0.5. y: 0.5 }] (옳은 테스트)", () => {
const givenData = initFindingGameState.payload;
const whenData: Action = action.click({ request: {
answerList: [
{ positionX: 0.5, positionY: 0.5 }
{ positionX: 0.5, positionY: 0.5 },
]
}, response: {
responseCode: 1000,
Expand All @@ -54,16 +54,17 @@ test("클릭 이벤트:입력값 [{x: 0.5 y: 0.5}, 틀린답 {x: 0.7, y: 0.1}] -
descriptionImageUrl: "",
title: "",
content: ""
}] })
}]})

tester.given(givenData).when(whenData).then(thenData, true);
});

test("클릭 이벤트:입력값 [{x: 0.5 y: 0.5}] -> 결과값 answers.length == 2, answers = [answer{ x: 0.5. y: 0.5 }] (옳은 테스트)", () => {
test("클릭 이벤트:입력값 [{x: 0.5 y: 0.5}, 틀린답 {x: 0.7, y: 0.1}] -> 결과값 answers.length == 1, answers = [answer{ x: 0.5. y: 0.5 }] (옳은 테스트)", () => {
const givenData = initFindingGameState.payload;
const whenData: Action = action.click({ request: {
answerList: [
{ positionX: 0.5, positionY: 0.5 },
{ positionX: 0.7, positionY: 0.1 }
]
}, response: {
responseCode: 1000,
Expand All @@ -89,7 +90,7 @@ test("클릭 이벤트:입력값 [{x: 0.5 y: 0.5}] -> 결과값 answers.length =
descriptionImageUrl: "",
title: "",
content: ""
}]})
}] })

tester.given(givenData).when(whenData).then(thenData, true);
});
Expand Down Expand Up @@ -134,13 +135,6 @@ test("클릭 이벤트:입력값 [{x: 0.5 y: 0.5}, 맞은답 {x: 0.7, y: 0.1}] -
descriptionImageUrl: "",
title: "",
content: ""
},
{
positionX: 0.7,
positionY: 0.1,
descriptionImageUrl: "",
title: "",
content: ""
}]})

tester.given(givenData).when(whenData).then(thenData, false);
Expand Down
53 changes: 53 additions & 0 deletions Caecae/src/shared/Hyundux-saga-test/SagaTest.ts
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)
}

0 comments on commit 403bf05

Please sign in to comment.