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

WIP: add tests by integrating mws + vitest #143

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
dc69b7d
wip
alessbell Aug 28, 2023
8fc8867
Add Persisted Queries to router and actions
michael-watson Aug 29, 2023
5c1a50c
Merge pull request #144 from apollographql/pq
michael-watson Aug 29, 2023
b7fd562
Update register_pq.yaml
michael-watson Aug 29, 2023
c4a1c56
Merge pull request #145 from apollographql/pq
michael-watson Aug 29, 2023
d5fe7ba
Fix FeaturedPlaylistConnection resolvers - return array
michael-watson Aug 29, 2023
477a916
Merge pull request #146 from apollographql/featured-playlist-connection
jerelmiller Aug 29, 2023
1d9c6cc
Update router.yaml
michael-watson Aug 30, 2023
a5a6a7f
Merge pull request #147 from apollographql/router-callback-hotfix
michael-watson Aug 30, 2023
77fdb07
Enable Subscriptions for latest router version
michael-watson Aug 30, 2023
912335e
Merge pull request #148 from apollographql/router-callback-hotfix
michael-watson Aug 30, 2023
5f5390c
Rollback Router to v1.23
michael-watson Aug 30, 2023
32fb13a
Merge pull request #149 from apollographql/router-rollback
michael-watson Aug 30, 2023
473d9ed
Update Dockerfile
michael-watson Aug 30, 2023
fda2eb1
Merge pull request #150 from apollographql/router-rollback
michael-watson Aug 30, 2023
e95f210
Bump Apollo Server to be same package
michael-watson Aug 31, 2023
d342569
Merge pull request #151 from apollographql/bump-apollo-server
michael-watson Aug 31, 2023
625b180
Add a couple of QoL tools to debug the router.
o0Ignition0o Sep 1, 2023
31d540f
stay on router 1.23 for now
o0Ignition0o Sep 1, 2023
c7a6fad
revert enabled:true to accomodate router 1.23.0
o0Ignition0o Sep 1, 2023
3c3d88b
Fix formatting in router.yaml
jerelmiller Sep 1, 2023
d4e23ba
Merge branch 'main' into igni/router_debug_and_headers_forwarding
jerelmiller Sep 1, 2023
a37e23c
chore: continue setup
alessbell Sep 6, 2023
2f037f5
chore: remove console.log
alessbell Sep 6, 2023
2346562
chore: use enums in mocks
alessbell Sep 7, 2023
094838a
Merge pull request #152 from o0Ignition0o/igni/router_debug_and_heade…
michael-watson Sep 7, 2023
d1d9d2f
chore: revert localStorage hack
alessbell Sep 13, 2023
9362528
Fix is_empty on null
michael-watson Sep 14, 2023
fa40823
Enable operation limits
michael-watson Sep 14, 2023
631c937
Update README.md
alessbell Sep 20, 2023
9c96e8c
Merge pull request #154 from apollographql/alessbell-patch-1
jerelmiller Sep 21, 2023
9b4d4fb
diagnose only one instance of graphql in the node_modules error
alessbell Sep 29, 2023
a6140e8
feat: set-up testing with graphql-tools/mock
alessbell Oct 3, 2023
7b9d641
feat: commit patches
alessbell Oct 3, 2023
5ecd0e7
feat: SchemaLink set-up
alessbell Oct 3, 2023
5bd6764
Merge branches 'main' and 'issue-53-add-tests' of https://github.com/…
codesandbot Oct 3, 2023
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
16 changes: 8 additions & 8 deletions .apollo/router-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ apq:

# # Uncomment 'preview_callback' to try HTTP callback protocal at the /graphql
# # https://www.apollographql.com/docs/router/executing-operations/subscription-callback-protocol
# subscription:
# mode:
# preview_callback:
# public_url: http://0.0.0.0:4000/callback # The router's public URL
# listen: 0.0.0.0:${env.PORT:-4000} # The IP address and port the router will listen on for subscription callbacks
# path: /callback # The path of the router's callback endpoint
# subgraphs: # The list of subgraphs that use the HTTP callback protocol
# - playback
subscription:
mode:
preview_callback:
public_url: http://127.0.0.1:4000 # The router's public URL
listen: 0.0.0.0:${env.PORT:-4000} # The IP address and port the router will listen on for subscription callbacks
path: /callback # The path of the router's callback endpoint
subgraphs: # The list of subgraphs that use the HTTP callback protocol
- playback
27 changes: 27 additions & 0 deletions .github/workflows/register_pq.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Register Persisted Queries
on:
push:
branches:
- main
paths:
- 'client/**'
- '.github/workflows/register_pq.yaml'
- 'persisted-query-manifest.config.ts'
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
publish_persisted_queries:
name: Publish Playback schema to GraphOS
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Rover
run: |
curl -sSL https://rover.apollo.dev/nix/latest | sh
echo "$HOME/.rover/bin" >> $GITHUB_PATH
- name: Publish Operations Manifest
run: rover persisted-queries publish $APOLLO_GRAPH_REF --manifest ./client/src/apollo/persisted-query-manifest.json
env:
APOLLO_KEY: ${{ secrets.APOLLO_KEY }}
APOLLO_VCS_COMMIT: ${{ github.event.pull_request.head.sha }}
APOLLO_GRAPH_REF: ${{ secrets.APOLLO_GRAPH_REF }}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# <a href="https://www.apollographql.com/"><img src="https://user-images.githubusercontent.com/841294/53402609-b97a2180-39ba-11e9-8100-812bab86357c.png" height="100" alt="Apollo logo"></a>
<div align="center">
<a href="https://www.apollographql.com/"><img src="https://raw.githubusercontent.com/apollographql/apollo-client-devtools/main/assets/apollo-wordmark.svg" height="100" alt="Apollo Client"></a>
</div>

# React + Apollo Spotify Showcase

Expand Down
28 changes: 17 additions & 11 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"node": ">=18"
},
"dependencies": {
"@apollo/client": "^3.8.0",
"@apollo/client": "^3.8.1",
"@apollo/persisted-query-lists": "^1.0.0",
"@radix-ui/react-context-menu": "^2.1.3",
"@radix-ui/react-dialog": "^1.0.3",
Expand All @@ -18,23 +18,23 @@
"@radix-ui/react-tabs": "^1.0.3",
"@radix-ui/react-tooltip": "^1.0.5",
"@tanstack/react-table": "^8.9.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2",
"@types/react": "^18.0.26",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.0.9",
"@wry/equality": "^0.5.3",
"classnames": "^2.3.2",
"date-fns": "^2.29.3",
"deepmerge": "^4.3.0",
"formik": "^2.2.9",
"graphql": "^16.8.1",
"graphql-ws": "^5.12.0",
"lodash": "^4.17.21",
"lucide-react": "^0.124.0",
"node-vibrant": "3.1.6",
"prism-react-renderer": "^1.3.5",
"prism-themes": "^1.9.0",
"prismjs": "^1.29.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-markdown": "^8.0.5",
Expand All @@ -49,7 +49,8 @@
"start": "vite",
"build": "tsc && vite build",
"serve": "serve dist -s -n -L -p $PORT",
"lint:types": "tsc --noEmit -p ./tsconfig.json"
"lint:types": "tsc --noEmit -p ./tsconfig.json",
"test": "NODE_ENV=test vitest"
},
"browserslist": {
"production": [
Expand All @@ -66,20 +67,25 @@
"devDependencies": {
"@apollo/rover": "^0.16.2",
"@mdx-js/rollup": "^2.3.0",
"@rollup/plugin-graphql": "^2.0.3",
"@tailwindcss/line-clamp": "^0.4.2",
"@types/mdx": "^2.0.3",
"@types/node": "^20.4.5",
"@types/node": "^20.5.7",
"@types/prismjs": "^1.26.0",
"@vitejs/plugin-react": "^3.1.0",
"@vitejs/plugin-react": "^4.0.4",
"autoprefixer": "^10.4.13",
"babel-plugin-prismjs": "^2.1.0",
"jsdom": "^22.1.0",
"msw": "^1.3.1",
"postcss": "^8.4.21",
"remark-frontmatter": "^4.0.1",
"remark-mdx-frontmatter": "^2.1.1",
"tailwindcss": "^3.2.7",
"type-fest": "^3.6.1",
"typescript": "^5.1.6",
"vite": "^4.4.4"
"typescript": "^5.2.2",
"vite": "^4.4.9",
"vitest": "^0.34.3",
"vitest-dom": "^0.1.0"
},
"volta": {
"node": "18.16.1"
Expand Down
8 changes: 8 additions & 0 deletions client/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { beforeAll, afterAll, afterEach } from 'vitest';
import { server } from './src/mocks/server';

import 'vitest-dom/extend-expect';

beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterAll(() => server.close());
afterEach(() => server.resetHandlers());
25 changes: 25 additions & 0 deletions client/src/__tests__/IndexRoute.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, test, describe } from 'vitest';
import { Suspense } from 'react';
import { render, screen } from '@testing-library/react';
import { ApolloProvider } from '@apollo/client';
import * as IndexRoute from '../routes/index';
import client from '../apollo/client';
import { BrowserRouter } from 'react-router-dom';

describe('IndexRoute', () => {
test('renders', async () => {
render(
<ApolloProvider client={client}>
<Suspense fallback={<IndexRoute.LoadingState />}>
<BrowserRouter>
<IndexRoute.RouteComponent />
</BrowserRouter>
</Suspense>
</ApolloProvider>
);

expect(await screen.findByText(/afternoon delight/i)).toBeInTheDocument();
expect(await screen.findByText(/this is my playlist/i)).toBeInTheDocument();
expect(await screen.findByText(/description/i)).toBeInTheDocument();
});
});
11 changes: 8 additions & 3 deletions client/src/apollo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import introspection from './introspection.json';
import libraryContains from '../fieldPolicies/libraryContains';
import offsetConnectionPagination from '../fieldPolicies/offsetConnectionPagination';
import cursorConnectionPagination from '../fieldPolicies/cursorConnectionPagination';
import { ExecutableSchemaLink } from '../mocks/handlers';
import { getAccessToken } from '../auth';
import { version } from '../../package.json';
import { persistedQueryModeVar } from '../vars';
Expand Down Expand Up @@ -68,9 +69,13 @@ const httpAuthLink = setContext(async ({ context }) => {
};
});

const httpLink = createHttpLink({
uri: import.meta.env.VITE_SERVER_HOST,
});
const httpLink = split(
() => process.env.NODE_ENV === 'test',
ExecutableSchemaLink,
createHttpLink({
uri: import.meta.env.VITE_SERVER_HOST || 'http://localhost:4000/',
})
);

export default new ApolloClient({
link: from([httpAuthLink, persistedQueries, httpLink]),
Expand Down
5 changes: 4 additions & 1 deletion client/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
refresh: STORAGE_KEYS.REFRESH_TOKEN,
} as const;

export const readToken = (tokenType: TokenType = 'access') => {

Check warning on line 17 in client/src/auth.ts

View workflow job for this annotation

GitHub Actions / ESLint

'tokenType' is assigned a value but never used
try {
return localStorage.getItem(TOKEN_TO_STORAGE_KEYS[tokenType]);
// console.log(localStorage.getItem(TOKEN_TO_STORAGE_KEYS[tokenType]));
// return localStorage.getItem(TOKEN_TO_STORAGE_KEYS[tokenType]);
// TODO: mock localStorage
return 'abc';
} catch (e) {
return null;
}
Expand Down
6 changes: 6 additions & 0 deletions client/src/graphql.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module '*.graphql' {
import { DocumentNode } from 'graphql';
const Schema: DocumentNode;

export = Schema;
}
50 changes: 50 additions & 0 deletions client/src/mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { rest } from 'msw';
import { buildSchema } from 'graphql';
import { addMocksToSchema, createMockStore } from '@graphql-tools/mock';
import { SchemaLink } from '@apollo/client/link/schema/index.js';
import SpotifySchema from '../../schema.graphql';

const mocks = {
Int: () => 6,
Float: () => 22.1,
String: () => 'string',
};

export const resolvers = {
FeaturedPlaylistConnection: {
message: () => 'Afternoon delight',
edges: () => [{}],
},
Image: {
url: () => 'https://spotify.com/image.png',
},
Playlist: {
id: () => 1,
name: () => 'This is my playlist',
description: () => 'A description',
},
};

const schema = buildSchema(SpotifySchema?.loc?.source.body || '');

const store = createMockStore({ schema, mocks });

export const ExecutableSchemaLink = new SchemaLink({
schema: addMocksToSchema({
schema,
store,
resolvers,
}),
});

export const handlers = [
rest.post('https://accounts.spotify.com/api/token', (req, res, ctx) => {
return res(
ctx.json({
expires_in: '10000000',
access_token: 'foo',
refresh_token: 'bar',
})
);
}),
];
5 changes: 5 additions & 0 deletions client/src/mocks/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { setupServer } from 'msw/node';
import { handlers } from './handlers';

// This configures a request mocking server with the given request handlers.
export const server = setupServer(...handlers);
1 change: 1 addition & 0 deletions client/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const PlaylistTileGrid = ({
queryRef: QueryReference<IndexRouteQuery>;
}) => {
const { data } = useReadQuery(queryRef);
// console.log(data.featuredPlaylists.edges);
return (
<div className={containerStyles}>
<PageTitle>{data.featuredPlaylists?.message}</PageTitle>
Expand Down
5 changes: 0 additions & 5 deletions client/src/setupTests.ts

This file was deleted.

2 changes: 1 addition & 1 deletion client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"outDir": "dist",
"jsx": "react-jsx"
},
"include": ["src"],
"include": ["src", "./setupTests.ts"],
"exclude": ["node_modules"]
}
15 changes: 15 additions & 0 deletions client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import graphql from '@rollup/plugin-graphql';

export default defineConfig(async () => {
const mdx = await import('@mdx-js/rollup');
Expand All @@ -12,6 +13,7 @@ export default defineConfig(async () => {
port: process.env.PORT ?? 3000,
},
plugins: [
graphql(),
mdx.default({
remarkPlugins: [
remarkFrontmatter.default,
Expand All @@ -38,5 +40,18 @@ export default defineConfig(async () => {
},
}),
],
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['./setupTests.ts'],
// solves CJS/ESM dual package hazard issue with graphql
// whereby some imports resolve with CJS version and some the ESM version
// see: https://github.com/vitejs/vite/issues/7879#issuecomment-1349079757
server: {
deps: {
fallbackCJS: true,
},
},
},
};
});
Loading
Loading