Skip to content

Commit

Permalink
Add snapshot test for attribute table
Browse files Browse the repository at this point in the history
Same data as the attribute table, but in the form of a snapshot. Uses
puppeteer to run headless Chromium.
  • Loading branch information
acdlite committed Sep 1, 2017
1 parent 46218a2 commit de3eb2e
Show file tree
Hide file tree
Showing 8 changed files with 12,212 additions and 14 deletions.
16 changes: 8 additions & 8 deletions fixtures/attribute-behavior/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ function getRenderedAttributeValues(attribute, type) {
ReactDOM15,
ReactDOMServer15,
attribute,
type,
type
);
const react16Value = getRenderedAttributeValue(
React,
ReactDOM16,
ReactDOMServer16,
attribute,
type,
type
);

let hasSameBehavior;
Expand Down Expand Up @@ -81,7 +81,7 @@ for (let attribute of attributes) {
res.canonicalDefaultValue,
res.didWarn,
res.didError,
].join('||'),
].join('||')
)
.join('||');
}
Expand Down Expand Up @@ -166,7 +166,7 @@ function ResultPopover(props) {
hasSameBehavior: props.hasSameBehavior,
},
null,
2,
2
)}
</pre>
);
Expand Down Expand Up @@ -382,7 +382,7 @@ class App extends Component {
componentWillMount() {
this.attributes = this.getAttributes(
this.state.sortOrder,
this.state.filter,
this.state.filter
);
}

Expand All @@ -395,7 +395,7 @@ class App extends Component {
this.attributes = this.getAttributes(
nextState.sortOrder,
nextState.filter,
nextState.completedHashes,
nextState.completedHashes
);
this.grid.forceUpdateGrids();
}
Expand Down Expand Up @@ -428,11 +428,11 @@ class App extends Component {
switch (sortOrder) {
case ALPHABETICAL:
return filteredAttributes.sort(
(attr1, attr2) => (attr1.name < attr2.name ? -1 : 1),
(attr1, attr2) => (attr1.name < attr2.name ? -1 : 1)
);
case REV_ALPHABETICAL:
return filteredAttributes.sort(
(attr1, attr2) => (attr1.name < attr2.name ? 1 : -1),
(attr1, attr2) => (attr1.name < attr2.name ? 1 : -1)
);
case GROUPED_BY_ROW_PATTERN: {
return filteredAttributes.sort((attr1, attr2) => {
Expand Down
6 changes: 3 additions & 3 deletions fixtures/attribute-behavior/src/attributeBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -2503,12 +2503,12 @@ function warn(str) {
_didWarn = true;
}
const UNKNOWN_HTML_TAGS = new Set(['keygen', 'time', 'command']);
function getRenderedAttributeValue(
export function getRenderedAttributeValue(
React,
renderer,
serverRenderer,
attribute,
type,
type
) {
const originalConsoleError = console.error;
console.error = warn;
Expand Down Expand Up @@ -2582,7 +2582,7 @@ function getRenderedAttributeValue(
let hasUnknownElement = false;
try {
const html = serverRenderer.renderToString(
React.createElement(tagName, props),
React.createElement(tagName, props)
);
container = createContainer();
container.innerHTML = html;
Expand Down
73 changes: 73 additions & 0 deletions fixtures/attribute-behavior/src/headless.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable */

import {
getCanonicalizedValue,
getRenderedAttributeValue,
attributes,
types,
} from './attributeBehavior';

const React = global.React;
const {Component} = React;

const ReactDOM16 = global.ReactDOM16;
const ReactDOMServer16 = global.ReactDOMServer16;

function getUniqueKey(t, key, depth = 1) {
if (depth !== 1) {
key = `${key} (variant ${depth})`;
}
if (t.has(key)) {
return getUniqueKey(t, key, depth + 1);
}
return key;
}

function test() {
let log = '';
for (let attribute of attributes) {
log += `${attribute.name}\n`;
for (let type of types) {
const result = getRenderedAttributeValue(
React,
ReactDOM16,
ReactDOMServer16,
attribute,
type
);

const {
didError,
didWarn,
canonicalResult,
canonicalDefaultValue,
ssrHasSameBehavior,
ssrHasSameBehaviorExceptWarnings,
} = result;

let descriptions = [];
if (canonicalResult === canonicalDefaultValue) {
descriptions.push('NO CHANGE');
}
if (didError) {
descriptions.push('ERROR');
}
if (didWarn) {
descriptions.push('WARN');
}
if (!ssrHasSameBehavior) {
if (ssrHasSameBehaviorExceptWarnings) {
descriptions.push('SSR WARNS');
} else {
descriptions.push('SSR DEVIATION');
}
}

log += `\t${type.name} -> ${canonicalResult} ${descriptions.join(', ')}\n`;
}
}

return log;
}

window.test = test;
5 changes: 5 additions & 0 deletions fixtures/attribute-behavior/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import './index.css';
const ReactDOM = (window.ReactDOM16 = window.ReactDOM);
window.ReactDOMServer16 = window.ReactDOMServer;

if (window.location.pathname === '/headless') {
import('./headless');
return;
}

const App = await import('./App');

ReactDOM.render(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"platform": "^1.1.0",
"prettier": "1.2.2",
"prop-types": "^15.5.8",
"puppeteer": "^0.10.1",
"rimraf": "^2.6.1",
"rollup": "^0.41.6",
"rollup-plugin-alias": "^1.2.1",
Expand Down
82 changes: 82 additions & 0 deletions src/renderers/dom/shared/__tests__/DOMAttributeBehavior-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/

'use strict';

const ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
const puppeteer = require('puppeteer');

let child;

describe('DOMAttributeBehavior', () => {
beforeEach(() => {
jest.resetModules();
});

afterEach(() => {
if (child) {
// Kill the child process and its subprocesses
process.kill(-child.pid, 'SIGINT');
}
});

async function testAttributeBehavior(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const result = await page.evaluate(() => {
return window.test();
});
expect(result).toMatchSnapshot();
}

it(
'works',
done => {
if (!ReactDOMFeatureFlags.useFiber) {
done();
return;
}
const path = require('path');
const {execSync, spawn} = require('child_process');

const cwd = path.resolve(process.cwd(), 'fixtures/attribute-behavior');

execSync('yarn', {cwd});

process.env.BROWSER = 'none';
process.env.PORT = 9292;

child = spawn('yarn', ['start'], {cwd, detached: true});

let didStartTest = false;
child.stdout.on('data', data => {
const str = data.toString('utf8');
if (str.includes('Failed')) {
console.error('Failure on CRA startup');
done();
return;
}

if (!didStartTest && str.includes('http://localhost:9292/')) {
didStartTest = true;
testAttributeBehavior('http://localhost:9292/headless')
.then(() => done())
.catch(error => {
console.error(error);
done();
});
}
});
},
30000,
);
});
Loading

0 comments on commit de3eb2e

Please sign in to comment.