Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Cómo probar la función del selector con redux-saga-test-plan

Estoy probando mi redux-saga-flow y tengo un problema al probar el método selector con select.

Selector

 const selector = (state) => { console.log("selector"); return data; }

Flujo de la saga Redux

 export function* testedSagaFlow() { const data = yield select(selector); if (!data) { return; } yield put (otherAction) ... }

Prueba de flujo

 test("Get data from store", () => { return expectSaga(testedSagaFlow) .provide([[mathchers.select.selector(selector), null]]) .select(selector) .not.put(otherAction) .run() })

Después de ejecutar una prueba, no tiene console.log("selector"); y también esta línea de código no está cubierta por la prueba.

¿Cómo puedo probar un selector?

Lo mismo no funciona con la prueba unitaria.

 test("Unit test flow", () => { const saga = testSaga(testedSagaFlow); saga .next() .select(selector) .next(null) .isDone() })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

"redux-saga-test-plan": "^4.0.1" .

Opción 1. Usar withState :

Para el estado estático, puede usar el método withState para permitir que funcionen los efectos select .

Opción 2. Usar proveedor estático

Puede proporcionar valores simulados de manera concisa a través de proveedores estáticos. Pase una matriz de pares de tuplas (pares de matrices) al método de provide . Para cada par, el primer elemento debe ser un comparador para igualar el efecto y el segundo efecto debe ser el valor simulado que desea proporcionar.

P.ej

saga.ts :

 import { put, select } from 'redux-saga/effects'; const otherAction = { type: 'OTHER_ACTION' }; export const selector = (state) => { console.log('selector'); return state.data; }; export function* testedSagaFlow() { const data = yield select(selector); if (!data) { return; } yield put(otherAction); }

saga.test.ts :

 import { expectSaga } from 'redux-saga-test-plan'; import { select } from 'redux-saga/effects'; import { selector, testedSagaFlow } from './saga'; describe('70199170', () => { test('should dispatch other action', () => { const state = { data: true }; return expectSaga(testedSagaFlow).withState(state).put({ type: 'OTHER_ACTION' }).run(); }); test('should return if data is nil', () => { const state = { data: null }; return expectSaga(testedSagaFlow).withState(state).not.put({ type: 'OTHER_ACTION' }).run(); }); }); describe('70199170 - use provider', () => { test('should dispatch other action', () => { return expectSaga(testedSagaFlow) .provide([[select(selector), true]]) .put({ type: 'OTHER_ACTION' }) .run(); }); test('should return if data is nil', () => { return expectSaga(testedSagaFlow) .provide([[select(selector), null]]) .not.put({ type: 'OTHER_ACTION' }) .run(); }); });

Resultado de la prueba:

 PASS redux-saga-examples packages/redux-saga-examples/src/stackoverflow/70199170/saga.test.ts 70199170 ✓ should dispatch other action (30 ms) ✓ should return if data is nil (4 ms) 70199170 - use provider ✓ should dispatch other action (2 ms) ✓ should return if data is nil (3 ms) console.log selector at selector (packages/redux-saga-examples/src/stackoverflow/70199170/saga.ts:6:11) console.log selector at selector (packages/redux-saga-examples/src/stackoverflow/70199170/saga.ts:6:11) Test Suites: 1 passed, 1 total Tests: 4 passed, 4 total Snapshots: 0 total Time: 2.934 s, estimated 3 s Ran all test suites related to changed files.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!