Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests for partial data #377

Merged
merged 6 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion integration/api/graphql.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ export const resolvers = {
},
user: async (_, args) => {
// simulate network delay
// await sleep(1000);
if (args.delay) {
await sleep(args.delay);
}

const user = getSnapshot(args.snapshot).find((c) => c.id === `${args.snapshot}:${args.id}`);

if (args.forceNullDate) {
user.birthDate = null;
}

if (!user) {
throw new GraphQLYogaError('User not found', { code: 404 });
}
Expand All @@ -75,6 +82,7 @@ export const resolvers = {
const [snapshot, id] = nodeID.split(':');
const list = getSnapshot(snapshot);
const user = list.find((u) => u.id === nodeID);

return {
...user,
__typename: 'User'
Expand Down
4 changes: 2 additions & 2 deletions integration/api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type PageInfo {
type Query {
avgYearsBirthDate: Float!
node(id: ID!): Node
user(id: ID!, snapshot: String!, tmp: Boolean): User!
user(id: ID!, snapshot: String!, tmp: Boolean, delay: Int, forceNullDate: Boolean): User!
usersConnection(
after: String
before: String
Expand All @@ -52,7 +52,7 @@ type Query {
}

type User implements Node {
birthDate: DateTime!
birthDate: DateTime
friendsConnection(after: String, before: String, first: Int, last: Int): UserConnection!
friendsList(limit: Int, offset: Int): [User!]!
id: ID!
Expand Down
4 changes: 4 additions & 0 deletions integration/houdini.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ const config = {
module: 'esm',
apiUrl: 'http://localhost:4000/graphql',
defaultCachePolicy: 'CacheOrNetwork',
defaultPartial: true,
routes: (filepath) => filepath && !filepath.includes('_') && !filepath.includes('+'),
scalars: {
DateTime: {
type: 'Date',
// turn the api's response into that type
unmarshal(val) {
if (val === null) {
AlecAivazis marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
return new Date(val);
},
// turn the value into something the API can use
Expand Down
1 change: 1 addition & 0 deletions integration/src/lib/utils/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const routes = {
Stores_Fragment_Null: '/stores/fragment-null',
Stores_Metadata: '/stores/metadata',

Stores_Partial_List: '/stores/partial/partial_List',
Stores_Pagination_query_forward_cursor: '/stores/pagination/query/forward-cursor',

Preprocess_query_simple: '/preprocess/query/simple',
Expand Down
8 changes: 8 additions & 0 deletions integration/src/routes/preprocess/query/scalars.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
`);
</script>

ISO:
<div id="result-date">
{$data?.user.birthDate.toISOString()}
</div>

<br />

Local:
<div id="result-date-local">
{$data?.user.birthDate.toLocaleString()}
</div>
6 changes: 6 additions & 0 deletions integration/src/routes/stores/partial/Partial_List.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query Partial_List {
usersList(snapshot: "Partial") @cache(partial: true) {
AlecAivazis marked this conversation as resolved.
Show resolved Hide resolved
id
name
}
}
7 changes: 7 additions & 0 deletions integration/src/routes/stores/partial/Partial_User.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query Partial_User($id: ID!) @cache(partial: true) {
user(id: $id, snapshot: "Partial", delay: 1000) {
id
name
birthDate
}
}
27 changes: 27 additions & 0 deletions integration/src/routes/stores/partial/partial.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { sleep } from '@kitql/helper';
import { test } from '@playwright/test';
import { routes } from '../../../lib/utils/routes.js';
import { expectToBe } from '../../../lib/utils/testsHelper.js';

test.describe('Partial Pages', () => {
test('From the list to the detail should see 2 info then the date coming', async ({ page }) => {
// Go to the list
await page.goto(routes.Stores_Partial_List);

// We should have the list
expectToBe(
page,
'Partial:1 - Bruce Willis Partial:2 - Samuel Jackson Partial:3 - Morgan Freeman Partial:4 - Tom Hanks'
);

// Click on the link and check directly the 3 divs
await page.locator('a[id="2"]').click();
expectToBe(page, 'Partial:2', 'div[id="id"]');
expectToBe(page, 'Samuel Jackson', 'div[id="name"]');
expectToBe(page, 'undefined', 'div[id="birthDate"]');

// Wait a bit so that the server respond and birthDate is displayed
sleep(2345);
expectToBe(page, '1948-12-21T00:00:00.000Z', 'div[id="birthDate"]');
});
});
28 changes: 28 additions & 0 deletions integration/src/routes/stores/partial/partial_List.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script context="module" lang="ts">
import { browser } from '$app/env';
import { GQL_Partial_List } from '$houdini';
import type { Load } from '@sveltejs/kit';

export const load: Load<{}, {}, {}> = async (event) => {
await GQL_Partial_List.fetch({ event });

return {
props: {}
};
};
</script>

<script lang="ts">
$: browser && GQL_Partial_List.fetch();
</script>

<div id="result">
{#each $GQL_Partial_List.data?.usersList ?? [] as { id, name }}
{@const realId = id.split(':')[1]}
<div>
<a id={realId} href={`./partial_${realId}`}>
{id} - {name}
</a>
</div>
{/each}
</div>
40 changes: 40 additions & 0 deletions integration/src/routes/stores/partial/partial_[userId].svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<script context="module" lang="ts">
import { browser } from '$app/env';
import { GQL_Partial_User, type Partial_User$input } from '$houdini';
import type { Load } from '@sveltejs/kit';

export const load: Load<{ userId: string }, {}, { variables: Partial_User$input }> = async (
event
) => {
const variables = { id: event.params.userId };

await GQL_Partial_User.fetch({ event, variables });

return {
props: {
variables
}
};
};
</script>

<script lang="ts">
export let variables: Partial_User$input;

$: browser && GQL_Partial_User.fetch({ variables });
</script>

<a href="./partial_List">Back to the list</a>

<br />
<br />

<div id="id">
{$GQL_Partial_User.data?.user.id}
</div>
<div id="name">
{$GQL_Partial_User.data?.user.name}
</div>
<div id="birthDate">
{$GQL_Partial_User.data?.user.birthDate?.toISOString()}
</div>