扫码一下
查看教程更方便
在 react 上下文之外操作和评估 recoil selectors 以进行测试是非常有用的。这可以通过使用 recoil snapshot 来实现。您可以使用 snapshot_unstable()
生成一个新的快照,然后使用该 snapshot 来评估用于测试的 selectors。
const numberstate = atom({key: 'number', default: 0});
const multipliedstate = selector({
key: 'multipliednumber',
get: ({get}) => get(numberstate) * 100,
});
test('test multipliedstate', () => {
const initialsnapshot = snapshot_unstable();
expect(initialsnapshot.getloadable(multipliedstate).valueorthrow()).tobe(0);
const testsnapshot = snapshot_unstable(({set}) => set(numberstate, 1));
expect(testsnapshot.getloadable(multipliedstate).valueorthrow()).tobe(100);
})