-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for styled-components SWC transform disabled (#31214)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ea88ef3
commit b5b315a
Showing
7 changed files
with
164 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { join } from 'path' | ||
import webdriver from 'next-webdriver' | ||
import { createNext, FileRef } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { fetchViaHTTP } from 'next-test-utils' | ||
|
||
describe('styled-components SWC transform', () => { | ||
let next: NextInstance | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: { | ||
'next.config.js': new FileRef( | ||
join(__dirname, 'styled-components-disabled/next.config.js') | ||
), | ||
pages: new FileRef(join(__dirname, 'styled-components-disabled/pages')), | ||
}, | ||
dependencies: { | ||
'styled-components': '5.3.3', | ||
}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
async function matchLogs$(browser) { | ||
let foundLog = false | ||
|
||
const browserLogs = await browser.log('browser') | ||
|
||
browserLogs.forEach((log) => { | ||
if (log.message.includes('Warning: Prop `%s` did not match.')) { | ||
foundLog = true | ||
} | ||
}) | ||
return foundLog | ||
} | ||
it('should have hydration mismatch with styled-components transform disabled', async () => { | ||
let browser | ||
try { | ||
browser = await webdriver(next.appPort, '/') | ||
|
||
// Compile /_error | ||
await fetchViaHTTP(next.appPort, '/404') | ||
|
||
try { | ||
// Try 4 times to be sure there is no mismatch | ||
expect(await matchLogs$(browser)).toBe(false) | ||
await browser.refresh() | ||
expect(await matchLogs$(browser)).toBe(false) | ||
await browser.refresh() | ||
expect(await matchLogs$(browser)).toBe(false) | ||
await browser.refresh() | ||
expect(await matchLogs$(browser)).toBe(false) | ||
throw new Error('did not find mismatch') | ||
} catch (err) { | ||
// Verify that it really has the logs | ||
expect(await matchLogs$(browser)).toBe(true) | ||
} | ||
} finally { | ||
if (browser) { | ||
await browser.close() | ||
} | ||
} | ||
}) | ||
}) |
1 change: 1 addition & 0 deletions
1
test/development/basic/styled-components-disabled/next.config.js
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 @@ | ||
module.exports = {} |
26 changes: 26 additions & 0 deletions
26
test/development/basic/styled-components-disabled/pages/_app.js
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,26 @@ | ||
import { createGlobalStyle, ThemeProvider } from 'styled-components' | ||
|
||
const GlobalStyle = createGlobalStyle` | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
box-sizing: border-box; | ||
} | ||
` | ||
|
||
const theme = { | ||
colors: { | ||
primary: '#0070f3', | ||
}, | ||
} | ||
|
||
export default function App({ Component, pageProps }) { | ||
return ( | ||
<> | ||
<GlobalStyle /> | ||
<ThemeProvider theme={theme}> | ||
<Component {...pageProps} /> | ||
</ThemeProvider> | ||
</> | ||
) | ||
} |
30 changes: 30 additions & 0 deletions
30
test/development/basic/styled-components-disabled/pages/_document.js
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,30 @@ | ||
import Document from 'next/document' | ||
import { ServerStyleSheet } from 'styled-components' | ||
|
||
export default class MyDocument extends Document { | ||
static async getInitialProps(ctx) { | ||
const sheet = new ServerStyleSheet() | ||
const originalRenderPage = ctx.renderPage | ||
|
||
try { | ||
ctx.renderPage = () => | ||
originalRenderPage({ | ||
enhanceApp: (App) => (props) => | ||
sheet.collectStyles(<App {...props} />), | ||
}) | ||
|
||
const initialProps = await Document.getInitialProps(ctx) | ||
return { | ||
...initialProps, | ||
styles: ( | ||
<> | ||
{initialProps.styles} | ||
{sheet.getStyleElement()} | ||
</> | ||
), | ||
} | ||
} finally { | ||
sheet.seal() | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
test/development/basic/styled-components-disabled/pages/index.js
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,38 @@ | ||
import styled, { css } from 'styled-components' | ||
const Button = styled.a` | ||
/* This renders the buttons above... Edit me! */ | ||
display: inline-block; | ||
border-radius: 3px; | ||
padding: 0.5rem 0; | ||
margin: 0.5rem 1rem; | ||
width: 11rem; | ||
background: transparent; | ||
color: white; | ||
border: 2px solid white; | ||
/* The GitHub button is a primary button | ||
* edit this to target it specifically! */ | ||
${(props) => | ||
props.primary && | ||
css` | ||
background: white; | ||
color: black; | ||
`} | ||
` | ||
|
||
export default function Home() { | ||
return ( | ||
<div> | ||
<Button | ||
href="https://github.com/styled-components/styled-components" | ||
target="_blank" | ||
rel="noopener" | ||
primary | ||
> | ||
GitHub | ||
</Button> | ||
|
||
<Button href="/docs">Documentation</Button> | ||
</div> | ||
) | ||
} |
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