Skip to content

Commit

Permalink
Use standard cookies with jsdom (fix #236 and #237)
Browse files Browse the repository at this point in the history
  • Loading branch information
eXon committed Dec 27, 2019
1 parent 41943ba commit 5939056
Show file tree
Hide file tree
Showing 5 changed files with 1,572 additions and 35 deletions.
7 changes: 6 additions & 1 deletion packages/react-cookie-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"license": "MIT",
"main": "server.js",
"scripts": {
"build": "webpack"
"build": "webpack",
"test": "jest"
},
"dependencies": {
"@babel/register": "^7.0.0",
Expand All @@ -22,7 +23,11 @@
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-stage-2": "^7.0.0",
"@testing-library/react": "^9.4.0",
"babel-jest": "^24.9.0",
"babel-loader": "^8.0.0",
"jest": "^24.9.0",
"react-test-renderer": "^16.12.0",
"webpack": "^4.16.5",
"webpack-cli": "^3.1.0"
}
Expand Down
15 changes: 15 additions & 0 deletions packages/react-cookie-demo/src/components/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { cleanup, fireEvent, render } from '@testing-library/react';
import App from './App';
import { Cookies } from 'react-cookie';

afterEach(cleanup);

test('Changing the name change the cookie value', () => {
const cookies = new Cookies();
const utils = render(<App />);
const input = utils.getByPlaceholderText('Enter your name');
fireEvent.change(input, { target: { value: 'Ben Solo' } });
expect(input.value).toBe('Ben Solo');
expect(cookies.get('name')).toBe('Ben Solo');
});
1 change: 1 addition & 0 deletions packages/react-cookie-demo/src/components/NameForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function NameForm({ name, onChange }) {
>
<input
type="text"
placeholder="Enter your name"
defaultValue={name}
onChange={e => onChange(e.target.value)}
/>
Expand Down
19 changes: 0 additions & 19 deletions packages/universal-cookie/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,10 @@ import * as cookie from 'cookie';
import { Cookie, CookieGetOptions, CookieParseOptions } from './types';

export function hasDocumentCookie() {
// JSDOM does not support changing cookies, disable it for tests
if (isJsDom()) {

This comment has been minimized.

Copy link
@morficus

morficus May 1, 2020

Just a thought... I would recommend adding something in the documentation about how this lib now deals with the JSDom environment. After updating to v4.0.3 I ran in to an issue with some tests failing (used server-side)... and after a lot of digging through this lib code changes (for what I thought would be a regression) I came across this commit.

The net is that anyone using the lib server-side and using Jest for unit tests will run in to false-failures unless they set explicitly set @jest-environment node in their tests (which I suspect a lot of people do not do)

return false;
}

// Can we get/set cookies on document.cookie?
return typeof document === 'object' && typeof document.cookie === 'string';
}

function isJsDom(): boolean {
if (
typeof navigator !== 'object' ||
typeof navigator.userAgent !== 'string'
) {
return false;
}

return (
navigator.userAgent.indexOf('Node.js') >= 0 ||
navigator.userAgent.indexOf('jsdom') >= 0
);
}

export function cleanCookies() {
document.cookie.split(';').forEach(function(c) {
document.cookie = c
Expand Down
Loading

0 comments on commit 5939056

Please sign in to comment.