【发布时间】:2018-09-12 20:12:30
【问题描述】:
我有一个表格。当用户从第 1 步转到第 2 步时,代码会检查第 1 步中的所有必填字段,然后再将用户移动到第 2 步。当用户在第 2、3 或 4 步并点击刷新或在新窗口中打开 url ,它们被重定向到介绍页面。
我的问题是测试代码和测试什么。
我正在使用 React、Redux、Jest 和 Sinon。
// actions.js
export const redirectToIntroPage = () => (dispatch, getState) => {
const fields = {
field1: getState().app.inputs.field1,
field2: getState().app.inputs.field2,
field3: getState().app.inputs.field3,
};
if (objHasNullValue(fields)) {
goToPage(PAGES.INTRODUCTION);
}
};
getState() 被多次调用,因为我只关心指定的键。 app 有一大堆我不需要的东西。
// parsing.js
export const objHasNullValue = obj => {
for (var property in obj) {
if (isNull(obj[property]) === true) {
return true;
}
}
return false;
};
goToPage 是不言自明的。
// action.test.js
describe('redirect to intro page', () => {
let sandbox, fakeDispatch, fakeState, goToPage;
const fakeGetState = () => (fakeState);
beforeEach(() => {
sandbox = sinon.createSandbox();
fakeDispatch = sandbox.stub();
goToPage = sandbox.spy();
});
afterEach(() => {
sandbox.restore();
});
it('should redirect if the Personalisation Form has a null value', () => {
fakeState = {
app: {
inputs: {
field1: null,
field2: null,
field3: null,
},
},
};
redirectToIntroPage()(fakeDispatch, fakeGetState);
expect(goToPage.calledOnce).toBe(true);
});
it('shouldn\'t redirect if the Personalisation Form is complete', () => {
});
});
我对测试的想法是删除goToPage 并检查它是否已被调用。我必须致电redirectToIntroPage()(fakeDispatch, fakeGetState) 才能这样做,但我如何进入redirectToIntroPage 以检查goToPage 是否已被调用?
我看过here,这在概念上是有道理的,但是我如何在Sinon和Jest中翻译它?
我也看了here,也是一样的想法。
【问题讨论】:
标签: javascript reactjs redux jestjs sinon