Skip to content

Commit b198d4e

Browse files
dougmacknzsapegin
authored andcommitted
Test: Add more Cypress tests for isolated mode toggling (styleguidist#1304)
1 parent a9e1985 commit b198d4e

File tree

17 files changed

+107
-29
lines changed

17 files changed

+107
-29
lines changed

examples/styled-components/babel.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ module.exports = {
55
{
66
modules: false,
77
useBuiltIns: 'usage',
8-
corejs: 3
8+
corejs: 3,
99
},
1010
],
1111
['@babel/preset-react'],
12-
['@babel/preset-typescript']
12+
['@babel/preset-typescript'],
1313
],
1414
plugins: ['@babel/plugin-proposal-class-properties', 'babel-plugin-styled-components'],
1515
};

src/client/rsg-components/Examples/Examples.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ExamplesRenderer from 'rsg-components/Examples/ExamplesRenderer';
66

77
export default function Examples({ examples, name, exampleMode }, { codeRevision }) {
88
return (
9-
<ExamplesRenderer>
9+
<ExamplesRenderer name={name}>
1010
{examples.map((example, index) => {
1111
switch (example.type) {
1212
case 'code':

src/client/rsg-components/Examples/ExamplesRenderer.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ const styles = () => ({
77
root: {},
88
});
99

10-
export function ExamplesRenderer({ classes, children }) {
11-
return <article className={classes.root}>{children}</article>;
10+
export function ExamplesRenderer({ classes, name, children }) {
11+
return (
12+
<article className={classes.root} data-testid={`${name}-examples`}>
13+
{children}
14+
</article>
15+
);
1216
}
1317

1418
ExamplesRenderer.propTypes = {
1519
classes: PropTypes.object.isRequired,
20+
name: PropTypes.string.isRequired,
1621
children: PropTypes.node,
1722
};
1823

src/client/rsg-components/Examples/__snapshots__/Examples.spec.js.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`should render examples 1`] = `
4-
<Styled(Examples)>
4+
<Styled(Examples)
5+
name="button"
6+
>
57
<Playground
68
code=
79
<button>

src/client/rsg-components/Playground/Playground.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class Playground extends Component {
8282
) : (
8383
<PlaygroundRenderer
8484
name={name}
85+
exampleIndex={index}
8586
preview={preview}
8687
previewProps={settings.props || {}}
8788
tabButtons={

src/client/rsg-components/Playground/Playground.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ it('renderer should render preview', () => {
127127
const actual = shallow(
128128
<PlaygroundRenderer
129129
classes={classes(styles)}
130+
exampleIndex={0}
130131
name="name"
131132
preview={<div>preview</div>}
132133
previewProps={{ className: 'pizza', title: 'salami' }}

src/client/rsg-components/Playground/PlaygroundRenderer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const styles = ({ space, color, borderRadius }) => ({
2828

2929
export function PlaygroundRenderer({
3030
classes,
31+
exampleIndex,
3132
name,
3233
preview,
3334
previewProps,
@@ -37,7 +38,7 @@ export function PlaygroundRenderer({
3738
}) {
3839
const { className, ...props } = previewProps;
3940
return (
40-
<div className={classes.root}>
41+
<div className={classes.root} data-testid={`${name}-example-${exampleIndex}`}>
4142
<div className={cx(classes.preview, className)} {...props} data-preview={name}>
4243
{preview}
4344
</div>
@@ -52,6 +53,7 @@ export function PlaygroundRenderer({
5253

5354
PlaygroundRenderer.propTypes = {
5455
classes: PropTypes.object.isRequired,
56+
exampleIndex: PropTypes.number.isRequired,
5557
name: PropTypes.string.isRequired,
5658
preview: PropTypes.node.isRequired,
5759
previewProps: PropTypes.object.isRequired,

src/client/rsg-components/Playground/__snapshots__/Playground.spec.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`renderer should render preview 1`] = `
44
<div
55
className="root"
6+
data-testid="name-example-0"
67
>
78
<div
89
className="preview pizza"
@@ -41,6 +42,7 @@ exports[`renderer should render preview 1`] = `
4142

4243
exports[`should render component renderer 1`] = `
4344
<Styled(Playground)
45+
exampleIndex={0}
4446
name="name"
4547
preview={
4648
<Preview

src/client/rsg-components/ReactComponent/ReactComponentRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function ReactComponentRenderer({
3939
tabBody,
4040
}) {
4141
return (
42-
<div className={classes.root} id={name + '-container'}>
42+
<div className={classes.root} data-testid={`${name}-container`}>
4343
<header className={classes.header}>
4444
{heading}
4545
{pathLine && <Pathline>{pathLine}</Pathline>}

src/client/rsg-components/ReactComponent/__snapshots__/ReactComponent.spec.js.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ exports[`ReactComponent should pass rendered description, usage, examples, etc.
207207

208208
exports[`ReactComponentRenderer should render component 1`] = `
209209
<div
210-
id="Test-container"
210+
data-testid="Test-container"
211211
>
212212
<header>
213213
<div>
@@ -222,7 +222,7 @@ exports[`ReactComponentRenderer should render component 1`] = `
222222

223223
exports[`ReactComponentRenderer should render component without a pathline 1`] = `
224224
<div
225-
id="Test-container"
225+
data-testid="Test-container"
226226
>
227227
<header>
228228
<div>
@@ -234,7 +234,7 @@ exports[`ReactComponentRenderer should render component without a pathline 1`] =
234234

235235
exports[`ReactComponentRenderer should render description 1`] = `
236236
<div
237-
id="Test-container"
237+
data-testid="Test-container"
238238
>
239239
<header>
240240
<div>
@@ -254,7 +254,7 @@ exports[`ReactComponentRenderer should render description 1`] = `
254254

255255
exports[`ReactComponentRenderer should render docs 1`] = `
256256
<div
257-
id="Test-container"
257+
data-testid="Test-container"
258258
>
259259
<header>
260260
<div>
@@ -274,7 +274,7 @@ exports[`ReactComponentRenderer should render docs 1`] = `
274274

275275
exports[`ReactComponentRenderer should render examples 1`] = `
276276
<div
277-
id="Test-container"
277+
data-testid="Test-container"
278278
>
279279
<header>
280280
<div>
@@ -299,7 +299,7 @@ exports[`ReactComponentRenderer should render examples 1`] = `
299299

300300
exports[`ReactComponentRenderer should render usage section 1`] = `
301301
<div
302-
id="Test-container"
302+
data-testid="Test-container"
303303
>
304304
<header>
305305
<div>

src/client/rsg-components/StyleGuide/StyleGuideRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function StyleGuideRenderer({
7575
</footer>
7676
</main>
7777
{hasSidebar && (
78-
<div className={classes.sidebar}>
78+
<div className={classes.sidebar} data-testid="sidebar">
7979
<header className={classes.logo}>
8080
<Logo>{title}</Logo>
8181
{version && <Version>{version}</Version>}

src/client/rsg-components/StyleGuide/__snapshots__/StyleGuide.spec.js.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ exports[`renderer should render logo, version, table of contents, ribbon and pas
1414
/>
1515
</footer>
1616
</main>
17-
<div>
17+
<div
18+
data-testid="sidebar"
19+
>
1820
<header>
1921
<Styled(Logo)>
2022
Hello

src/client/rsg-components/ToolbarButton/ToolbarButtonRenderer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function ToolbarButtonRenderer({
4646
href,
4747
title,
4848
small,
49+
testId,
4950
children,
5051
}) {
5152
const classNames = cx(classes.button, className, {
@@ -54,7 +55,7 @@ export function ToolbarButtonRenderer({
5455

5556
if (href !== undefined) {
5657
return (
57-
<a href={href} title={title} className={classNames} aria-label={title}>
58+
<a href={href} title={title} className={classNames} aria-label={title} data-testid={testId}>
5859
{children}
5960
</a>
6061
);
@@ -74,6 +75,7 @@ ToolbarButtonRenderer.propTypes = {
7475
onClick: PropTypes.func,
7576
title: PropTypes.string,
7677
small: PropTypes.bool,
78+
testId: PropTypes.string,
7779
children: PropTypes.node,
7880
};
7981

src/client/rsg-components/slots/IsolateButton.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@ import MdFullscreenExit from 'react-icons/lib/md/fullscreen-exit';
55
import ToolbarButton from 'rsg-components/ToolbarButton';
66
import getUrl from '../../utils/getUrl';
77

8-
const IsolateButton = ({ name, example, isolated }) =>
9-
isolated ? (
8+
const IsolateButton = ({ name, example, isolated }) => {
9+
const testID = example ? `${name}-${example}-isolate-button` : `${name}-isolate-button`;
10+
11+
return isolated ? (
1012
<ToolbarButton
1113
href={getUrl({ anchor: true, slug: name.toLowerCase() })}
1214
title="Show all components"
15+
testId={testID}
1316
>
1417
<MdFullscreenExit />
1518
</ToolbarButton>
1619
) : (
17-
<ToolbarButton href={getUrl({ name, example, isolated: true })} title="Open isolated">
20+
<ToolbarButton
21+
href={getUrl({ name, example, isolated: true })}
22+
title="Open isolated"
23+
testId={testID}
24+
>
1825
<MdFullscreen />
1926
</ToolbarButton>
2027
);
28+
};
2129

2230
IsolateButton.propTypes = {
2331
name: PropTypes.string.isRequired,

src/client/rsg-components/slots/__snapshots__/IsolateButton.spec.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`should renderer a link home in isolated mode 1`] = `
44
<Styled(ToolbarButton)
55
href="/#pizza"
6+
testId="Pizza-isolate-button"
67
title="Show all components"
78
>
89
<MdFullscreenExit />
@@ -12,6 +13,7 @@ exports[`should renderer a link home in isolated mode 1`] = `
1213
exports[`should renderer a link to example isolated mode 1`] = `
1314
<Styled(ToolbarButton)
1415
href="/#!/Pizza/3"
16+
testId="Pizza-3-isolate-button"
1517
title="Open isolated"
1618
>
1719
<MdFullscreen />
@@ -21,6 +23,7 @@ exports[`should renderer a link to example isolated mode 1`] = `
2123
exports[`should renderer a link to isolated mode 1`] = `
2224
<Styled(ToolbarButton)
2325
href="/#!/Pizza"
26+
testId="Pizza-isolate-button"
2427
title="Open isolated"
2528
>
2629
<MdFullscreen />

test/cypress/integration/component_spec.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe('Single component', () => {
22
before(() => {
33
// Open simple button component in isolation
4-
cy.visit('/#!/Button/1');
4+
cy.visit('/#!/Button');
55
});
66

77
describe('props and methods section', () => {
@@ -35,10 +35,12 @@ describe('Single component', () => {
3535

3636
describe('preview section', () => {
3737
beforeEach(() => {
38-
cy.get('[class^=rsg--preview]')
39-
.as('preview')
40-
.closest('[class^=rsg--root]')
38+
cy.get('[data-testid*="-example-"]')
4139
.as('container')
40+
.find('[class^=rsg--preview]')
41+
.as('preview');
42+
43+
cy.get('@container')
4244
.find('button')
4345
.contains('View Code')
4446
.as('viewCodeBtn');
@@ -79,5 +81,34 @@ describe('Single component', () => {
7981
.contains('Push Me Harder')
8082
.should('exist');
8183
});
84+
85+
it('toggles isolated example mode correctly', () => {
86+
cy.get('[data-testid$="-examples"]').as('componentExamples');
87+
88+
// Toggle into isolated example mode
89+
cy.get('@componentExamples')
90+
.find('[data-testid$="-isolate-button"]')
91+
.first()
92+
.click();
93+
94+
// Assert that there is only one example showing
95+
cy.get('@componentExamples')
96+
.find('[data-testid*="-example-"]')
97+
.should('have.length', 1);
98+
99+
// Toggle out of isolated example mode
100+
cy.get('@componentExamples')
101+
.find('[data-testid$="-isolate-button"]')
102+
.click();
103+
104+
// Assert the other examples are showing again
105+
cy.get('@componentExamples')
106+
.find('[data-testid*="-example-"]')
107+
.should('have.length.above', 1);
108+
109+
// Check that we've returned to isolated component mode instead of normal mode
110+
// TODO: this is currently bugged (returns to normal mode rather than isolated component mode)
111+
//cy.get('[id$=container]').should('have.length', 1);
112+
});
82113
});
83114
});

test/cypress/integration/core_spec.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
describe('Styleguidist core', () => {
2-
beforeEach(() => cy.visit('/'));
2+
before(() => cy.visit('/'));
33

44
it('loads the page', () => {
55
cy.title().should('include', 'React Styleguidist');
66
});
77

88
it('shows multiple components in normal mode', () => {
9-
cy.get('[id$=container]').should('have.length.above', 1);
9+
cy.get('[data-testid$=-container]').should('have.length.above', 1);
1010
});
1111

12-
it('shows single component in isolated mode', () => {
13-
cy.get('a[aria-label="Open isolated"]')
12+
it('toggles isolated component mode correctly', () => {
13+
cy.get('[data-testid=sidebar]').as('sidebar');
14+
15+
// Toggle into isolated mode
16+
cy.get('[data-testid$="-isolate-button"]')
17+
.first()
18+
.click();
19+
20+
// Assert there's only one component showing
21+
cy.get('[data-testid$=-container]').should('have.length', 1);
22+
23+
// Assert the sidebar is no longer showing
24+
cy.get('@sidebar').should('not.exist');
25+
26+
// Toogle out of isolated mode
27+
cy.get('[data-testid$="-isolate-button"]')
1428
.first()
1529
.click();
16-
cy.get('[id$=container]').should('have.length', 1);
30+
31+
// Assert that more than one component is now showing
32+
cy.get('[data-testid$=-container]').should('have.length.above', 1);
33+
34+
// Asser that the sidebar is now showing again
35+
cy.get('@sidebar').should('exist');
1736
});
1837
});

0 commit comments

Comments
 (0)