Created
July 15, 2019 18:17
-
-
Save radzserg/e535fa3b346383d6cabb88523a6c6d3e to your computer and use it in GitHub Desktop.
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
| // ISaga.ts | |
| export default interface ISaga { | |
| register(): Iterable<any>; | |
| } | |
| export default class ProductUpdatePageSaga implements ISaga { | |
| private readonly backend: IBackendApi; | |
| private readonly history: History; | |
| constructor(backend: IBackendApi, history: History) { | |
| this.backend = backend; | |
| this.history = history; | |
| } | |
| *register(): Iterable<any> { | |
| yield takeEvery(SAVE_PRODUCT, this.saveProduct()); | |
| } | |
| saveProduct = () => { | |
| const { backend, history } = this; // it's a little bit tricky with TS and no-invalid-this | |
| return function*(action: ISaveProductAction) { | |
| const productResult = backendApi.saveProduct(action.productId, action.productData); | |
| yield put(saveUserDetails(userDetails)); | |
| history.push("/products"); | |
| return true; | |
| }; | |
| }; | |
| } | |
| // test | |
| it("saves product", () => { | |
| const push = jest.fn(); | |
| const history = ({ push } as unknown) as History; | |
| const saveProduct = jest.fn().mockReturnValue({success: true}); | |
| const backendApi = ({ saveProduct } as unknown) as IBackendApi; | |
| const productData = {... }; | |
| const action: ISaveProduct = { | |
| type: SAVE_PRODUCT, | |
| productData, | |
| }; | |
| const saga = new Saga(backendApi, history); // inject | |
| const gen = saga.saveProduct()(action); | |
| const next = gen.next(); | |
| expect(saveProduct).toHaveBeenCalledWith({success: true}); | |
| expect(push).toHaveBeenCalledWith("/products"); | |
| expect(next.done).toBeTruthy(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment