Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
better server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Seccafien committed Sep 26, 2019
1 parent 81dfb15 commit 072e926
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
5 changes: 3 additions & 2 deletions packages/react-cookie/src/CookieUniversalProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ const NO_MANAGER_ERROR = [

export interface Props {
children?: React.ReactNode;
server?: boolean;
}

export function CookieUniversalProvider({children}: Props) {
export function CookieUniversalProvider({server, children}: Props) {
const manager = useNetworkManager();

const cookieManager = useLazyRef(() => {
if (hasDocumentCookie()) {
if (!server && hasDocumentCookie()) {
return new BrowserCookieManager();
}

Expand Down
1 change: 1 addition & 0 deletions packages/react-cookie/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export {
} from './types';

export {clearCookies, createCookies} from './tests/utilities';
export {hasDocumentCookie} from './utilities';
34 changes: 17 additions & 17 deletions packages/react-server/src/server/test/server.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import React from 'react';
import {useTitle} from '@shopify/react-html';
import {
useCookie,
createCookies,
CookieUniversalProvider,
} from '@shopify/react-cookie';
import {useCookie, CookieUniversalProvider} from '@shopify/react-cookie';
import {createServer} from '../server';

import {mockMiddleware, TestRack} from '../../test/utilities';
Expand Down Expand Up @@ -71,18 +67,22 @@ describe('createServer()', () => {
return <>{cookieValue}</>;
}

createCookies({foo: {value: 'bar'}});

const wrapper = await rack.mount(({ip, port}) =>
createServer({
port,
ip,
render: () => (
<CookieUniversalProvider>
<MockApp />
</CookieUniversalProvider>
),
}),
const wrapper = await rack.mount(
({ip, port}) =>
createServer({
port,
ip,
render: () => (
<CookieUniversalProvider server>
<MockApp />
</CookieUniversalProvider>
),
}),
{
headers: {
cookie: `${cookie}=${value}`,
},
},
);

const response = await wrapper.request();
Expand Down
7 changes: 5 additions & 2 deletions packages/react-server/src/test/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ export class TestRack {
this.servers.forEach(server => server.close());
}

async mount(mountFunction: ({port: number, ip: string}) => Server) {
async mount(
mountFunction: ({port: number, ip: string}) => Server,
options: RequestInit = {},
) {
const port = await getPort();
const ip = 'http://localhost';
const server = mountFunction({port, ip});

this.servers.push(server);

return {
request: () => fetch(`${ip}:${port}`).then(response => response),
request: () => fetch(`${ip}:${port}`, options).then(response => response),
};
}
}
Expand Down

0 comments on commit 072e926

Please sign in to comment.