Skip to content

Commit

Permalink
test: added spec
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Apr 4, 2023
1 parent 62e5c56 commit bf4064b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/villus/test/useQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
PostsQueryWithDescription,
} from './mocks/queries';
import { graphql } from 'msw';
import { DocumentTypeDecoration } from '@graphql-typed-document-node/core';

interface Post {
id: number;
Expand Down Expand Up @@ -86,6 +87,55 @@ describe('useQuery()', () => {
});
});

test('accepts typed document strings', async () => {
class TypedDocumentString<TResult, TVariables>
extends String
implements DocumentTypeDecoration<TResult, TVariables>
{
__apiType?: DocumentTypeDecoration<TResult, TVariables>['__apiType'];

constructor(private value: string, public __meta__?: { hash: string }) {
super(value);
}

toString(): string & DocumentTypeDecoration<TResult, TVariables> {
return this.value;
}
}

mount({
setup() {
useClient({
url: 'https://test.com/graphql',
});

const { data } = useQuery({
query: new TypedDocumentString(`
query Posts {
posts {
id
title
}
}
`),
});

return { data };
},
template: `
<div>
<ul v-if="data">
<li v-for="post in data.posts" :key="post.id">{{ post.title }}</li>
</ul>
</div>`,
});

await flushPromises();
await waitForExpect(() => {
expect(document.querySelectorAll('li').length).toBe(5);
});
});

test('caches queries by default', async () => {
mount({
setup() {
Expand Down

0 comments on commit bf4064b

Please sign in to comment.