import { nextTestSetup } from 'e2e-utils' import { check } from 'next-test-utils' describe('app dir - css', () => { const { next, isNextDev, skipped } = nextTestSetup({ files: __dirname, skipDeployment: true, dependencies: { '@picocss/pico': '1.5.7', sass: 'latest', '@next/mdx': 'canary', }, }) if (skipped) { return } describe('css support', () => { describe('server layouts', () => { it.skip('should support global css inside server layouts', async () => { const browser = await next.browser('/dashboard') // Should body text in red await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('.p')).color` ), 'rgb(255, 0, 0)' ) // Should inject global css for .green selectors await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('.green')).color` ), 'rgb(0, 128, 0)' ) }) it('should support css modules inside server layouts', async () => { const browser = await next.browser('/css/css-nested') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('#server-cssm')).color` ), 'rgb(0, 128, 0)' ) }) it('should support external css imports', async () => { const browser = await next.browser('/css/css-external') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('main')).paddingTop` ), '80px' ) }) }) describe('server pages', () => { it('should support global css inside server pages', async () => { const browser = await next.browser('/css/css-page') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('h1')).color` ), 'rgb(255, 0, 0)' ) }) it('should support css modules inside server pages', async () => { const browser = await next.browser('/css/css-page') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('#cssm')).color` ), 'rgb(0, 0, 255)' ) }) it('should not contain pages css in app dir page', async () => { const html = await next.render('/css/css-page') expect(html).not.toContain('/pages/_app.css') }) it('should support css modules shared between server pages', async () => { const browser = await next.browser('/css/css-page-shared-loading') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('#cssm')).color` ), 'rgb(0, 0, 255)' ) }) }) describe('client layouts', () => { it('should support css modules inside client layouts', async () => { const browser = await next.browser('/client-nested') // Should render h1 in red await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('h1')).color` ), 'rgb(255, 0, 0)' ) }) it('should support global css inside client layouts', async () => { const browser = await next.browser('/client-nested') // Should render button in red await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('button')).color` ), 'rgb(255, 0, 0)' ) }) }) describe('client pages', () => { it('should support css modules inside client pages', async () => { const browser = await next.browser('/client-component-route') // Should render p in red await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('p')).color` ), 'rgb(255, 0, 0)' ) }) it('should support global css inside client pages', async () => { const browser = await next.browser('/client-component-route') // Should render `b` in blue await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('b')).color` ), 'rgb(0, 0, 255)' ) }) }) describe('client components', () => { it('should support css modules inside client page', async () => { const browser = await next.browser('/css/css-client') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('#css-modules')).fontSize` ), '100px' ) }) it('should support css modules inside client components', async () => { const browser = await next.browser('/css/css-client/inner') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('#client-component')).fontSize` ), '100px' ) }) }) describe('special entries', () => { it('should include css imported in loading.js', async () => { const $ = await next.render$('/loading-bug/hi') // The link tag should be hoist into head with precedence properties const styles = $('head link[data-precedence]').length expect(styles).toBe(2) expect($('body h2').text()).toBe('Loading...') }) it('should include css imported in client template.js', async () => { const browser = await next.browser('/template/clientcomponent') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('button')).fontSize` ), '100px' ) }) it('should include css imported in server template.js', async () => { const browser = await next.browser('/template/servercomponent') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('h1')).color` ), 'rgb(255, 0, 0)' ) }) it('should include css imported in client not-found.js', async () => { const browser = await next.browser('/not-found/clientcomponent') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('h1')).color` ), 'rgb(255, 0, 0)' ) }) it('should include css imported in server not-found.js', async () => { const browser = await next.browser('/not-found/servercomponent') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('h1')).color` ), 'rgb(255, 0, 0)' ) }) it('should include root layout css for root not-found.js', async () => { const browser = await next.browser('/this-path-does-not-exist') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('h1')).color` ), 'rgb(210, 105, 30)' ) }) it('should include css imported in root not-found.js', async () => { const browser = await next.browser('/random-non-existing-path') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('h1')).color` ), 'rgb(210, 105, 30)' ) await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('h1')).backgroundColor` ), 'rgb(0, 0, 0)' ) }) it('should include css imported in error.js', async () => { const browser = await next.browser('/error/client-component') await browser.elementByCss('button').click() // Wait for error page to render and CSS to be loaded await new Promise((resolve) => setTimeout(resolve, 2000)) await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('button')).fontSize` ), '50px' ) }) }) describe('page extensions', () => { it('should include css imported in MDX pages', async () => { const browser = await next.browser('/mdx') await check( async () => await browser.eval( `window.getComputedStyle(document.querySelector('h1')).color` ), 'rgb(255, 0, 0)' ) }) }) describe('chunks', () => { it('should bundle css resources into chunks', async () => { const html = await next.render('/dashboard') const stylesheets = [ ...html.matchAll( / { it('should have inner layers take precedence over outer layers', async () => { const browser = await next.browser('/ordering') expect( await browser.eval( `window.getComputedStyle(document.querySelector('h1')).color` ) ).toBe('rgb(255, 0, 0)') expect( await browser.eval( `window.getComputedStyle(document.querySelector('h2')).color` ) ).toBe('rgb(255, 0, 0)') }) }) if (isNextDev) { it('should not affect css orders during HMR', async () => { const filePath = 'app/ordering/page.js' const origContent = await next.readFile(filePath) // h1 should be red const browser = await next.browser('/ordering') expect( await browser.eval( `window.getComputedStyle(document.querySelector('h1')).color` ) ).toBe('rgb(255, 0, 0)') expect( await browser.eval( `window.getComputedStyle(document.querySelector('h2')).color` ) ).toBe('rgb(255, 0, 0)') try { await next.patchFile( filePath, origContent.replace('