Skip to content

Commit ab171b5

Browse files
committed
[params] 100% coverage
[state] useValueState
1 parent 0245e49 commit ab171b5

3 files changed

Lines changed: 183 additions & 1 deletion

File tree

coverage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"tests":7244,"assertions":32227,"lines":{"total":2401,"covered":2400,"skipped":0,"pct":99.95},"statements":{"total":2598,"covered":2597,"skipped":0,"pct":99.96},"functions":{"total":1041,"covered":1040,"skipped":0,"pct":99.9},"branches":{"total":908,"covered":906,"skipped":0,"pct":99.77},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
1+
{"tests":7250,"assertions":32247,"lines":{"total":2401,"covered":2401,"skipped":0,"pct":100},"statements":{"total":2598,"covered":2598,"skipped":0,"pct":100},"functions":{"total":1041,"covered":1041,"skipped":0,"pct":100},"branches":{"total":908,"covered":908,"skipped":0,"pct":100},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}

test/unit/core/other/queries.test.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5042,6 +5042,124 @@ describe('Parameterized', () => {
50425042

50435043
expect(queries.getResultTable('q1')).toEqual({r1: {c1: 'a', c2: 'odd'}});
50445044
});
5045+
5046+
test('changes param from string to array and back', () => {
5047+
queries.setQueryDefinition(
5048+
'q1',
5049+
't1',
5050+
({select, where, param}) => {
5051+
select('c1');
5052+
where((getTableCell) => {
5053+
const p = param('p1');
5054+
return Array.isArray(p)
5055+
? (p as string[]).includes(getTableCell('c1') as string)
5056+
: getTableCell('c1') === p;
5057+
});
5058+
},
5059+
{p1: 'a'},
5060+
);
5061+
5062+
expect(queries.getResultTable('q1')).toEqual({r1: {c1: 'a'}});
5063+
5064+
queries.setParamValue('q1', 'p1', ['a', 'c', 'e']);
5065+
5066+
expect(queries.getResultTable('q1')).toEqual({
5067+
r1: {c1: 'a'},
5068+
r3: {c1: 'c'},
5069+
r5: {c1: 'e'},
5070+
});
5071+
5072+
queries.setParamValue('q1', 'p1', 'b');
5073+
5074+
expect(queries.getResultTable('q1')).toEqual({r2: {c1: 'b'}});
5075+
});
5076+
5077+
test('changes param from one array to another array', () => {
5078+
queries.setQueryDefinition(
5079+
'q1',
5080+
't1',
5081+
({select, where, param}) => {
5082+
select('c1');
5083+
where((getTableCell) => {
5084+
const p = param('p1') as string[];
5085+
return p.includes(getTableCell('c1') as string);
5086+
});
5087+
},
5088+
{p1: ['a', 'c']},
5089+
);
5090+
5091+
expect(queries.getResultTable('q1')).toEqual({
5092+
r1: {c1: 'a'},
5093+
r3: {c1: 'c'},
5094+
});
5095+
5096+
queries.setParamValue('q1', 'p1', ['b', 'd']);
5097+
5098+
expect(queries.getResultTable('q1')).toEqual({
5099+
r2: {c1: 'b'},
5100+
r4: {c1: 'd'},
5101+
});
5102+
5103+
queries.setParamValue('q1', 'p1', ['a', 'c']);
5104+
5105+
expect(queries.getResultTable('q1')).toEqual({
5106+
r1: {c1: 'a'},
5107+
r3: {c1: 'c'},
5108+
});
5109+
});
5110+
5111+
test('setting array param to equivalent array does not trigger', () => {
5112+
const listener = vi.fn();
5113+
queries.setQueryDefinition(
5114+
'q1',
5115+
't1',
5116+
({select, where, param}) => {
5117+
select('c1');
5118+
where((getTableCell) => {
5119+
const p = param('p1') as string[];
5120+
return p.includes(getTableCell('c1') as string);
5121+
});
5122+
},
5123+
{p1: ['a', 'c']},
5124+
);
5125+
5126+
queries.addResultTableListener('q1', listener);
5127+
5128+
expect(queries.getResultTable('q1')).toEqual({
5129+
r1: {c1: 'a'},
5130+
r3: {c1: 'c'},
5131+
});
5132+
5133+
queries.setParamValue('q1', 'p1', ['a', 'c']);
5134+
5135+
expect(listener).not.toHaveBeenCalled();
5136+
expect(queries.getResultTable('q1')).toEqual({
5137+
r1: {c1: 'a'},
5138+
r3: {c1: 'c'},
5139+
});
5140+
});
5141+
5142+
test('setting primitive param to same value does not trigger', () => {
5143+
const listener = vi.fn();
5144+
queries.setQueryDefinition(
5145+
'q1',
5146+
't1',
5147+
({select, where, param}) => {
5148+
select('c1');
5149+
where((getTableCell) => getTableCell('c1') === param('p1'));
5150+
},
5151+
{p1: 'a'},
5152+
);
5153+
5154+
queries.addResultTableListener('q1', listener);
5155+
5156+
expect(queries.getResultTable('q1')).toEqual({r1: {c1: 'a'}});
5157+
5158+
queries.setParamValue('q1', 'p1', 'a');
5159+
5160+
expect(listener).not.toHaveBeenCalled();
5161+
expect(queries.getResultTable('q1')).toEqual({r1: {c1: 'a'}});
5162+
});
50455163
});
50465164

50475165
describe('setParamValues', () => {
@@ -5098,6 +5216,33 @@ describe('Parameterized', () => {
50985216

50995217
expect(queries.getResultRowIds('q1')).toEqual(['r2']);
51005218
});
5219+
5220+
test('deletes param when missing from setParamValues', () => {
5221+
queries.setQueryDefinition(
5222+
'q1',
5223+
't1',
5224+
({select, where, param}) => {
5225+
select('c1');
5226+
where(
5227+
(getTableCell) =>
5228+
getTableCell('c1') === param('p1') ||
5229+
getTableCell('c2') === param('p2'),
5230+
);
5231+
},
5232+
{p1: 'a', p2: 'even'},
5233+
);
5234+
5235+
expect(queries.getResultTable('q1')).toEqual({
5236+
r1: {c1: 'a'},
5237+
r2: {c1: 'b'},
5238+
r4: {c1: 'd'},
5239+
});
5240+
5241+
queries.setParamValues('q1', {p1: 'c'});
5242+
5243+
expect(queries.getResultTable('q1')).toEqual({r3: {c1: 'c'}});
5244+
expect(queries.getParamValues('q1')).toEqual({p1: 'c'});
5245+
});
51015246
});
51025247

51035248
describe('Listeners', () => {

test/unit/core/ui-react/hooks.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,6 +2612,43 @@ describe('Read Hooks', () => {
26122612
unmount();
26132613
});
26142614

2615+
test('useParamValue with array changes', () => {
2616+
const queries = createQueries(store).setQueryDefinition(
2617+
'q1',
2618+
't1',
2619+
({select, where, param}) => {
2620+
select('c1');
2621+
where((getTableCell) => {
2622+
const p = param('p1');
2623+
return Array.isArray(p)
2624+
? (p as string[]).includes(getTableCell('c1') as string)
2625+
: getTableCell('c1') === p;
2626+
});
2627+
},
2628+
{p1: 'a'},
2629+
);
2630+
const Test = ({queryId, paramId}: {queryId: Id; paramId: Id}) =>
2631+
didRender(JSON.stringify(useParamValue(queryId, paramId, queries)));
2632+
const {container, rerender, unmount} = render(
2633+
<Test queryId="q1" paramId="p1" />,
2634+
);
2635+
2636+
expect(container.textContent).toEqual('"a"');
2637+
2638+
act(() => queries.setParamValue('q1', 'p1', ['a', 'c']));
2639+
expect(container.textContent).toEqual('["a","c"]');
2640+
2641+
act(() => queries.setParamValue('q1', 'p1', ['b', 'd']));
2642+
expect(container.textContent).toEqual('["b","d"]');
2643+
2644+
act(() => queries.setParamValue('q1', 'p1', 'e'));
2645+
expect(container.textContent).toEqual('"e"');
2646+
2647+
expect(didRender).toHaveBeenCalledTimes(4);
2648+
2649+
unmount();
2650+
});
2651+
26152652
test('useSetParamValueCallback', () => {
26162653
const queries = createQueries(store);
26172654
queries.setQueryDefinition(

0 commit comments

Comments
 (0)