Skip to content

Commit

Permalink
Test-case for sourced fields mess in attached operations (#329)
Browse files Browse the repository at this point in the history
Write test-case for bug
  • Loading branch information
igorkamyshev authored Aug 23, 2023
1 parent ca20587 commit 6de823f
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion packages/core/src/attach/__tests__/attach.query.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { allSettled, createStore, fork } from 'effector';
import { allSettled, createEvent, createStore, fork, sample } from 'effector';
import { describe, test, expect, vi } from 'vitest';
import { unknownContract } from '../../contract/unknown_contract';
import { fetchFx } from '../../fetch/fetch';
import { withFactory } from '../../libs/patronus';
import { createJsonQuery } from '../../query/create_json_query';

import { createQuery } from '../../query/create_query';
import { declareParams } from '../../remote_operation/params';
import { attachOperation } from '../attach';

describe('attach for query', () => {
Expand Down Expand Up @@ -187,4 +188,41 @@ describe('attach for query', () => {
expect(fetchMock).toBeCalledTimes(1);
expect(await fetchMock.mock.calls[0][0].url).toBe('https://api.salo.com/');
});

test('do not mess sourced fields, issue #327', async () => {
const originalHandler = vi.fn();
const originalQuery = createJsonQuery({
params: declareParams<{ id: number }>(),
request: {
method: 'GET',
url: (params) => `https://api.salo.com/${params.id}`,
},
response: {
contract: unknownContract,
},
});

const firstQuery = attachOperation(originalQuery);
const secondQuery = attachOperation(originalQuery);

const start = createEvent();

sample({ clock: start, fn: () => ({ id: 1 }), target: firstQuery.refresh });
sample({
clock: start,
fn: () => ({ id: 2 }),
target: secondQuery.refresh,
});

const scope = fork({
handlers: [[originalQuery.__.executeFx, originalHandler]],
});

await allSettled(start, { scope });

expect(originalHandler).toBeCalledTimes(2);

expect(originalHandler.mock.calls[0][0].url).toBe('https://api.salo.com/1');
expect(originalHandler.mock.calls[1][0].url).toBe('https://api.salo.com/2');
});
});

0 comments on commit 6de823f

Please sign in to comment.