Skip to content

Commit

Permalink
chore: add test for multi pk update
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe committed Feb 14, 2024
1 parent c9219d9 commit c8daa62
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
"ci:release": "changeset publish"
},
"devDependencies": {
"@changesets/cli": "2.27.0",
"eslint": "8.54.0",
"prettier": "3.1.0",
"@changesets/cli": "2.27.0",
"supabase": "^1.142.2",
"turbo": "1.10.16"
},
"engines": {
Expand Down
6 changes: 4 additions & 2 deletions packages/postgrest-core/src/postgrest-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ export class PostgrestFilter<Result extends Record<string, unknown>> {
}

applyFiltersOnPaths(obj: unknown, paths: string[]): obj is Result {
const filterFns = filterFilterDefinitionsByPaths(
const filteredFilters = filterFilterDefinitionsByPaths(
this.params.filters,
paths,
).map((d) => this.buildFilterFn(d));
);
if (filteredFilters.length === 0) return false;
const filterFns = filteredFilters.map((d) => this.buildFilterFn(d));
const filtersFn = (obj: unknown): obj is Result =>
filterFns.every((fn) => isObject(obj) && fn(obj));
return filtersFn(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,68 @@ describe('useUpdateMutation', () => {
provider = new Map();
});

it('should update existing cache item with multi primary key', async () => {
const NAME_1 = `${testRunPrefix}-1`;
const NAME_2 = `${testRunPrefix}-2`;

function Page() {
const [success, setSuccess] = useState<boolean>(false);
const { data } = useQuery(
client
.from('multi_pk')
.select('id_1,id_2,name')
.in('name', [NAME_1, NAME_2]),
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
},
);
const { trigger: insert } = useInsertMutation(client.from('multi_pk'), [
'id_1',
'id_2',
]);
const { trigger: update } = useUpdateMutation(
client.from('multi_pk'),
['id_1', 'id_2'],
null,
{
onSuccess: () => setSuccess(true),
},
);
return (
<div>
<div
data-testid="insert"
onClick={async () =>
await insert([{ id_1: 0, id_2: 0, name: NAME_1 }])
}
/>
<div
data-testid="update"
onClick={async () =>
await update({
id_1: 0,
id_2: 0,
name: NAME_2,
})
}
/>
<span>
{data?.find((d) => [NAME_1, NAME_2].includes(d.name ?? ''))?.name}
</span>
<span data-testid="success">{`success: ${success}`}</span>
</div>
);
}

renderWithConfig(<Page />, { provider: () => provider });
fireEvent.click(screen.getByTestId('insert'));
await screen.findByText(NAME_1, {}, { timeout: 10000 });
fireEvent.click(screen.getByTestId('update'));
await screen.findByText(NAME_2, {}, { timeout: 10000 });
await screen.findByText('success: true', {}, { timeout: 10000 });
});

it('should update existing cache item with serial primary key', async () => {
const VALUE_1 = `${testRunPrefix}-1`;
const VALUE_2 = `${testRunPrefix}-2`;
Expand Down
163 changes: 163 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c8daa62

Please sign in to comment.