-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #617 from Financial-Times/CPP-465-improve-x-intera…
…ction-test-coverage CP-465 Added e2e test for hydration and serialisation
- Loading branch information
Showing
13 changed files
with
201 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
**/public-prod/** | ||
**/blueprints/** | ||
web/static/** | ||
/e2e/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ bower_components | |
npm-debug.log | ||
.DS_Store | ||
dist | ||
.idea | ||
.idea | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// set up app to host main.js file included in server side rendered html | ||
const express = require('express') | ||
const server = express() | ||
server.use(express.static(__dirname)) | ||
exports.app = server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const { withActions, registerComponent } = require('@financial-times/x-interaction') | ||
const { h } = require('@financial-times/x-engine') | ||
|
||
export const greetingActions = withActions({ | ||
actionOne() { | ||
return { greeting: 'world' } | ||
} | ||
}) | ||
|
||
export const GreetingComponent = greetingActions(({ greeting, actions }) => { | ||
return ( | ||
<div className="greeting-text"> | ||
hello {greeting} | ||
<button className="greeting-button" onClick={actions.actionOne}> | ||
click to add to hello | ||
</button> | ||
</div> | ||
) | ||
}) | ||
|
||
registerComponent(GreetingComponent, 'GreetingComponent') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
const { h } = require('@financial-times/x-engine') // required for <GreetingComponent> | ||
const { Serialiser, HydrationData } = require('@financial-times/x-interaction') | ||
const puppeteer = require('puppeteer') | ||
const ReactDOMServer = require('react-dom/server') | ||
const express = require('express') | ||
import React from 'react' | ||
import { GreetingComponent } from './common' | ||
|
||
describe('x-interaction-e2e', () => { | ||
let browser | ||
let page | ||
let app | ||
let server | ||
|
||
beforeAll(async () => { | ||
app = express() | ||
server = app.listen(3004) | ||
app.use(express.static(__dirname)) | ||
browser = await puppeteer.launch() | ||
page = await browser.newPage() | ||
}) | ||
|
||
it('attaches the event listener to SSR components on hydration', async () => { | ||
const ClientComponent = () => { | ||
// main.js is the transpiled version of index.js, which contains the registered GreetingComponent, and invokes hydrate | ||
return <script type="module" src="./main.js" charset="utf-8"></script> | ||
} | ||
|
||
const serialiser = new Serialiser() | ||
const htmlString = ReactDOMServer.renderToString( | ||
<> | ||
<GreetingComponent serialiser={serialiser} /> | ||
<HydrationData serialiser={serialiser} /> | ||
<ClientComponent /> | ||
</> | ||
) | ||
|
||
app.get('/', (req, res) => { | ||
res.send(htmlString) | ||
}) | ||
|
||
// go to page and click button | ||
await page.goto('http://localhost:3004') | ||
await page.waitForSelector('.greeting-button') | ||
await page.click('.greeting-button') | ||
const text = await page.$eval('.greeting-text', (e) => e.textContent) | ||
expect(text).toContain('hello world') | ||
}) | ||
|
||
afterAll(async () => { | ||
try { | ||
;(await browser) && browser.close() | ||
await server.close() | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { hydrate } from '@financial-times/x-interaction' | ||
import './common' | ||
|
||
document.addEventListener('DOMContentLoaded', hydrate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = { | ||
testMatch: ['<rootDir>/e2e.test.js'], | ||
testPathIgnorePatterns: ['/node_modules/', '/bower_components/'], | ||
transform: { | ||
'^.+\\.jsx?$': '../packages/x-babel-config/jest' | ||
}, | ||
moduleNameMapper: { | ||
'^[./a-zA-Z0-9$_-]+\\.scss$': '<rootDir>/__mocks__/styleMock.js' | ||
}, | ||
testEnvironment: 'node' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "x-dash-e2e", | ||
"version": "0.0.0", | ||
"private": "true", | ||
"description": "This module enables you to write x-dash components that respond to events and change their own data.", | ||
"keywords": [ | ||
"x-dash" | ||
], | ||
"author": "", | ||
"license": "ISC", | ||
"x-dash": { | ||
"engine": { | ||
"server": { | ||
"runtime": "react", | ||
"factory": "createElement", | ||
"component": "Component", | ||
"fragment": "Fragment", | ||
"renderModule": "react-dom/server", | ||
"render": "renderToStaticMarkup" | ||
}, | ||
"browser": "react" | ||
} | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Financial-Times/x-dash.git" | ||
}, | ||
"engines": { | ||
"node": "12.x" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"devDependencies": { | ||
"puppeteer": "^10.4.0", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2", | ||
"webpack": "^5.54.0", | ||
"webpack-cli": "^4.8.0", | ||
"@financial-times/x-engine": "file:../packages/x-engine", | ||
"@financial-times/x-interaction": "file:../components/x-interaction" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const path = require('path') | ||
const xEngine = require('../packages/x-engine/src/webpack') | ||
const webpack = require('webpack') | ||
|
||
module.exports = { | ||
entry: './index.js', | ||
output: { | ||
filename: 'main.js', | ||
path: path.resolve(__dirname) | ||
}, | ||
plugins: [ | ||
new webpack.ProvidePlugin({ | ||
React: 'react' | ||
}), | ||
xEngine() | ||
], | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.(js|jsx)$/, | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
presets: ['@babel/preset-env', '@babel/preset-react'] | ||
} | ||
}, | ||
exclude: /node_modules/ | ||
} | ||
] | ||
}, | ||
resolve: { | ||
extensions: ['.js', '.jsx', '.json', '.wasm', '.mjs', '*'] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters