-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(emotion): fix array handling used in breakpoints utilities
Closes #29
- Loading branch information
Showing
9 changed files
with
185 additions
and
127 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 |
---|---|---|
@@ -1,14 +1,18 @@ | ||
import { css as emCss } from '@emotion/core' | ||
import { transform } from '@xstyled/core' | ||
|
||
export function css(...rawArgs) { | ||
const emCssArgs = emCss(...rawArgs) | ||
function styleToString(style, props) { | ||
if (Array.isArray(style)) | ||
return style.reduce((str, style) => str + styleToString(style, props), '') | ||
if (typeof style === 'function') return styleToString(style(props), props) | ||
return style | ||
} | ||
|
||
export function css(strings, ...rawArgs) { | ||
const emCssArgs = emCss(strings, ...rawArgs) | ||
const transformedStyles = transform(emCssArgs.styles) | ||
return p => ({ | ||
return props => ({ | ||
...emCssArgs, | ||
styles: transformedStyles.reduce((str, style) => { | ||
const value = typeof style === 'function' ? style(p) : style | ||
return str + value | ||
}, ''), | ||
styles: styleToString(transformedStyles, props), | ||
}) | ||
} |
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
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,117 @@ | ||
import React from 'react' | ||
import { render, cleanup } from '@testing-library/react' | ||
import { breakpoints, up, down, between } from '../../src' | ||
|
||
afterEach(cleanup) | ||
|
||
export function testBreakpoints({ styled, css }) { | ||
describe('#breakpoints', () => { | ||
it('should work with `css`', () => { | ||
const Dummy = styled.div` | ||
${breakpoints({ | ||
md: css` | ||
color: red; | ||
`, | ||
})} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 768px)', | ||
}) | ||
}) | ||
|
||
it('should work with object style', () => { | ||
const Dummy = styled.div` | ||
${breakpoints({ | ||
md: { color: 'red' }, | ||
})} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 768px)', | ||
}) | ||
}) | ||
}) | ||
|
||
describe('#up', () => { | ||
it('should work with `css`', () => { | ||
const Dummy = styled.div` | ||
${up( | ||
'md', | ||
css` | ||
color: red; | ||
`, | ||
)} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 768px)', | ||
}) | ||
}) | ||
|
||
it('should work with object style', () => { | ||
const Dummy = styled.div` | ||
${up('md', { color: 'red' })} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 768px)', | ||
}) | ||
}) | ||
}) | ||
|
||
describe('#down', () => { | ||
it('should work with `css`', () => { | ||
const Dummy = styled.div` | ||
${down( | ||
'md', | ||
css` | ||
color: red; | ||
`, | ||
)} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(max-width: 767.98px)', | ||
}) | ||
}) | ||
|
||
it('should work with object style', () => { | ||
const Dummy = styled.div` | ||
${down('md', { color: 'red' })} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(max-width: 767.98px)', | ||
}) | ||
}) | ||
}) | ||
|
||
describe('#between', () => { | ||
it('should work with `css`', () => { | ||
const Dummy = styled.div` | ||
${between( | ||
'sm', | ||
'md', | ||
css` | ||
color: red; | ||
`, | ||
)} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 576px) and (max-width: 767.98px)', | ||
}) | ||
}) | ||
|
||
it('should work with object style', () => { | ||
const Dummy = styled.div` | ||
${between('sm', 'md', { color: 'red' })} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 576px) and (max-width: 767.98px)', | ||
}) | ||
}) | ||
}) | ||
} |
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,8 @@ | ||
import { matchers } from 'jest-emotion' | ||
import styled from '@emotion/styled' | ||
import { css } from '@emotion/core' | ||
import { testBreakpoints } from '../common/breakpoints' | ||
|
||
expect.extend(matchers) | ||
|
||
testBreakpoints({ styled, css }) |
116 changes: 2 additions & 114 deletions
116
packages/system/tests/styled-components/breakpoints.test.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 |
---|---|---|
@@ -1,117 +1,5 @@ | ||
import React from 'react' | ||
import 'jest-styled-components' | ||
import { render, cleanup } from '@testing-library/react' | ||
import styled, { css } from 'styled-components' | ||
import { breakpoints, up, down, between } from '../../src' | ||
import { testBreakpoints } from '../common/breakpoints' | ||
|
||
afterEach(cleanup) | ||
|
||
describe('#breakpoints', () => { | ||
it('should work with `css`', () => { | ||
const Dummy = styled.div` | ||
${breakpoints({ | ||
md: css` | ||
color: red; | ||
`, | ||
})} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 768px)', | ||
}) | ||
}) | ||
|
||
it('should work with object style', () => { | ||
const Dummy = styled.div` | ||
${breakpoints({ | ||
md: { color: 'red' }, | ||
})} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 768px)', | ||
}) | ||
}) | ||
}) | ||
|
||
describe('#up', () => { | ||
it('should work with `css`', () => { | ||
const Dummy = styled.div` | ||
${up( | ||
'md', | ||
css` | ||
color: red; | ||
`, | ||
)} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 768px)', | ||
}) | ||
}) | ||
|
||
it('should work with object style', () => { | ||
const Dummy = styled.div` | ||
${up('md', { color: 'red' })} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 768px)', | ||
}) | ||
}) | ||
}) | ||
|
||
describe('#down', () => { | ||
it('should work with `css`', () => { | ||
const Dummy = styled.div` | ||
${down( | ||
'md', | ||
css` | ||
color: red; | ||
`, | ||
)} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(max-width: 767.98px)', | ||
}) | ||
}) | ||
|
||
it('should work with object style', () => { | ||
const Dummy = styled.div` | ||
${down('md', { color: 'red' })} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(max-width: 767.98px)', | ||
}) | ||
}) | ||
}) | ||
|
||
describe('#between', () => { | ||
it('should work with `css`', () => { | ||
const Dummy = styled.div` | ||
${between( | ||
'sm', | ||
'md', | ||
css` | ||
color: red; | ||
`, | ||
)} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 576px) and (max-width: 767.98px)', | ||
}) | ||
}) | ||
|
||
it('should work with object style', () => { | ||
const Dummy = styled.div` | ||
${between('sm', 'md', { color: 'red' })} | ||
` | ||
const { container } = render(<Dummy />) | ||
expect(container.firstChild).toHaveStyleRule('color', 'red', { | ||
media: '(min-width: 576px) and (max-width: 767.98px)', | ||
}) | ||
}) | ||
}) | ||
testBreakpoints({ styled, css }) |
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,7 @@ | ||
import { matchers } from 'jest-emotion' | ||
import styled, { css } from '@xstyled/emotion' | ||
import { testBreakpoints } from '../common/breakpoints' | ||
|
||
expect.extend(matchers) | ||
|
||
testBreakpoints({ styled, css }) |
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