@yoroi/common
Version:
The Common package of Yoroi SDK
66 lines (65 loc) • 2.77 kB
JavaScript
;
var _reactQuery = require("@tanstack/react-query");
var _reactNative = require("@testing-library/react-native");
var React = _interopRequireWildcard(require("react"));
var _useMutationWithInvalidations = require("./useMutationWithInvalidations");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const mutationFn = () => Promise.resolve(true);
describe('useMutationWithInvalidations', () => {
let queryClient;
beforeEach(() => {
queryClient = getMockedQueryClient();
});
afterEach(() => {
queryClient.clear();
});
it('should cancel and invalidate queries', async () => {
const queries = [['query1'], ['query2']];
const wrapper = props => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactQuery.QueryClientProvider, {
...props,
client: queryClient
});
const {
result
} = (0, _reactNative.renderHook)(() => (0, _useMutationWithInvalidations.useMutationWithInvalidations)({
mutationFn,
invalidateQueries: queries
}), {
wrapper
});
await (0, _reactNative.act)(async () => {
result.current.mutate(undefined);
});
await (0, _reactNative.act)(async () => {
await (0, _reactNative.waitFor)(() => result.current.isSuccess);
});
expect(queryClient.cancelQueries).toHaveBeenCalledTimes(2);
expect(queryClient.cancelQueries).toHaveBeenNthCalledWith(1, {
queryKey: queries[0]
});
expect(queryClient.cancelQueries).toHaveBeenNthCalledWith(2, {
queryKey: queries[1]
});
expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(2);
expect(queryClient.invalidateQueries).toHaveBeenNthCalledWith(1, {
queryKey: queries[0]
});
expect(queryClient.invalidateQueries).toHaveBeenNthCalledWith(2, {
queryKey: queries[1]
});
});
});
const getMockedQueryClient = () => {
const queryClient = new _reactQuery.QueryClient();
queryClient.cancelQueries = jest.fn();
queryClient.invalidateQueries = jest.fn();
queryClient.setDefaultOptions({
queries: {
retry: false,
gcTime: 0
}
});
return queryClient;
};
//# sourceMappingURL=useMutationWithInvalidations.test.js.map