教程 > recoil.js > 阅读:25

recoil 测试——迹忆客-ag捕鱼王app官网

在 react 之外测试 recoil selectors

在 react 上下文之外操作和评估 recoil selectors 以进行测试是非常有用的。这可以通过使用 recoil snapshot 来实现。您可以使用 snapshot_unstable() 生成一个新的快照,然后使用该 snapshot 来评估用于测试的 selectors。

示例: jest 单元测试 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);
})

查看笔记

扫码一下
查看教程更方便
网站地图