Skip to content

Commit

Permalink
Merge pull request #391 from Keithcat767/test/many-to-many-cache-inva…
Browse files Browse the repository at this point in the history
…lidation

test: many-to-many revalidateRelations test
  • Loading branch information
psteinroe authored Feb 29, 2024
2 parents 6c84008 + d9d8cbf commit 8d41dc8
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/postgrest-react-query/__tests__/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,48 @@ export type Json =
export interface Database {
public: {
Tables: {
address_book: {
Row: {
id: string;
created_at: string;
name: string | null;
};
Insert: {
name?: string | null;
};
Update: {
name?: string | null;
};
Relationships: [];
};
address_book_contact: {
Row: {
address_book: string;
contact: string;
};
Insert: {
address_book: string;
contact: string;
};
Update: {
address_book: string;
contact: string;
};
Relationships: [
{
foreignKeyName: 'address_book_contact_address_book_fkey';
columns: ['address_book'];
referencedRelation: 'address_book';
referencedColumns: ['id'];
},
{
foreignKeyName: 'address_book_contact_contact_fkey';
columns: ['contact'];
referencedRelation: 'contact';
referencedColumns: ['id'];
},
];
};
contact: {
Row: {
age_range: unknown | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,96 @@ describe('useDeleteMutation', () => {
contacts = data as Database['public']['Tables']['contact']['Row'][];
});

it('should invalidate address_book cache after delete', async () => {
const { data: addressBooks } = await client
.from('address_book')
.insert([
{
name: 'hello',
},
])
.select('id');

const addressBookId = addressBooks ? addressBooks[0].id : '';

await client.from('address_book_contact').insert([
{
address_book: addressBookId,
contact: contacts[0].id,
},
{
address_book: addressBookId,
contact: contacts[1].id,
},
]);

const queryClient = new QueryClient();
function Page() {
const { data: addressBookAndContact } = useQuery(
client
.from('address_book')
.select('id, name, contacts:contact (id, username)')
.eq('id', addressBookId)
.single(),
);

const { mutateAsync: deleteContactFromAddressBook } = useDeleteMutation(
client.from('address_book_contact'),
['contact', 'address_book'],
'contact, address_book',
{
revalidateRelations: [
{
relation: 'address_book',
relationIdColumn: 'id',
fKeyColumn: 'address_book',
},
],
},
);

return (
<div>
{addressBookAndContact?.name}
<span data-testid="count">
count: {addressBookAndContact?.contacts.length}
</span>
{addressBookAndContact?.contacts.map((contact) => {
return (
<div key={contact.id} data-testid="contact">
{contact.username}
<button
onClick={() =>
deleteContactFromAddressBook({
contact: contact.id,
address_book: addressBookAndContact.id,
})
}
>
Delete Contact
</button>
</div>
);
})}
</div>
);
}

renderWithConfig(<Page />, queryClient);

await screen.findByText(`hello`, {}, { timeout: 10000 });

await screen.findByText(`count: 2`, {}, { timeout: 10000 });

const deleteButtons = screen.getAllByRole(`button`, {
name: /Delete Contact/i,
});

fireEvent.click(deleteButtons[0]);

await screen.findByText(`count: 1`, {}, { timeout: 10000 });
});

it('should delete existing cache item and reduce count', async () => {
const queryClient = new QueryClient();
function Page() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
create table "public"."address_book" (
"id" uuid not null default gen_random_uuid(),
"created_at" timestamp with time zone not null default now(),
"name" character varying
);


create table "public"."address_book_contact" (
"contact" uuid not null,
"address_book" uuid not null
);

CREATE UNIQUE INDEX address_book_contact_pkey ON public.address_book_contact USING btree (contact, address_book);

CREATE UNIQUE INDEX address_book_pkey ON public.address_book USING btree (id);

alter table "public"."address_book" add constraint "address_book_pkey" PRIMARY KEY using index "address_book_pkey";

alter table "public"."address_book_contact" add constraint "address_book_contact_pkey" PRIMARY KEY using index "address_book_contact_pkey";

alter table "public"."address_book_contact" add constraint "address_book_contact_address_book_fkey" FOREIGN KEY (address_book) REFERENCES address_book(id) on delete cascade not valid;

alter table "public"."address_book_contact" validate constraint "address_book_contact_address_book_fkey";

alter table "public"."address_book_contact" add constraint "address_book_contact_contact_fkey" FOREIGN KEY (contact) REFERENCES contact(id) on delete cascade not valid;

alter table "public"."address_book_contact" validate constraint "address_book_contact_contact_fkey";

0 comments on commit 8d41dc8

Please sign in to comment.