Skip to content

Commit 4d91ba6

Browse files
committed
fix test setup to accurately reflect things
1 parent 1416b0f commit 4d91ba6

File tree

5 files changed

+81
-21
lines changed

5 files changed

+81
-21
lines changed

src/utils/options.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import styledPkg from 'styled-components/package.json'
2-
31
function getOption({ opts }, name, defaultValue = true) {
42
return opts[name] === undefined || opts[name] === null
53
? defaultValue
@@ -19,4 +17,7 @@ export const useTranspileTemplateLiterals = state =>
1917
* ahead of time.
2018
*/
2119
export const useNoTags = () =>
22-
parseInt(styledPkg.version.split('.')[0], 10) >= 4
20+
parseInt(
21+
require('styled-components/package.json').version.split('.')[0],
22+
10
23+
) >= 4
Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`should swap out the import if styled-components v4+ is detected 1`] = `
4-
"\\"use strict\\";
5-
6-
var _styledComponents = _interopRequireDefault(require(\\"styled-components\\"));
3+
exports[`should not swap out the import if styled-components v3 is detected 1`] = `
4+
"import styled from 'styled-components';
5+
const Named = styled(\\"div\\").withConfig({
6+
displayName: \\"example__Named\\",
7+
componentId: \\"ghf2ss-0\\"
8+
})([\\"width:100%;\\"]);
9+
const NamedWithInterpolation = styled(\\"div\\").withConfig({
10+
displayName: \\"example__NamedWithInterpolation\\",
11+
componentId: \\"ghf2ss-1\\"
12+
})([\\"color:\\", \\";\\"], props => props.color);
13+
const Wrapped = styled(Inner).withConfig({
14+
displayName: \\"example__Wrapped\\",
15+
componentId: \\"ghf2ss-2\\"
16+
})([\\"color:red;\\"]);"
17+
`;
718

8-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19+
exports[`should swap out the import if styled-components v4 beta is detected 1`] = `
20+
"import styled from \\"styled-components/no-tags\\";
21+
const Named = styled(\\"div\\").withConfig({
22+
displayName: \\"example__Named\\",
23+
componentId: \\"ghf2ss-0\\"
24+
})([\\"width:100%;\\"]);
25+
const NamedWithInterpolation = styled(\\"div\\").withConfig({
26+
displayName: \\"example__NamedWithInterpolation\\",
27+
componentId: \\"ghf2ss-1\\"
28+
})([\\"color:\\", \\";\\"], props => props.color);
29+
const Wrapped = styled(Inner).withConfig({
30+
displayName: \\"example__Wrapped\\",
31+
componentId: \\"ghf2ss-2\\"
32+
})([\\"color:red;\\"]);"
33+
`;
934

10-
var Named = _styledComponents.default.div\`
11-
width: 100%;
12-
\`;
13-
var NamedWithInterpolation = _styledComponents.default.div\`
14-
color: \${function (props) {
15-
return props.color;
16-
}};
17-
\`;
18-
var Wrapped = (0, _styledComponents.default)(Inner)\`
19-
color: red;
20-
\`;"
35+
exports[`should swap out the import if styled-components v4+ is detected 1`] = `
36+
"import styled from \\"styled-components/no-tags\\";
37+
const Named = styled(\\"div\\").withConfig({
38+
displayName: \\"example__Named\\",
39+
componentId: \\"ghf2ss-0\\"
40+
})([\\"width:100%;\\"]);
41+
const NamedWithInterpolation = styled(\\"div\\").withConfig({
42+
displayName: \\"example__NamedWithInterpolation\\",
43+
componentId: \\"ghf2ss-1\\"
44+
})([\\"color:\\", \\";\\"], props => props.color);
45+
const Wrapped = styled(Inner).withConfig({
46+
displayName: \\"example__Wrapped\\",
47+
componentId: \\"ghf2ss-2\\"
48+
})([\\"color:red;\\"]);"
2149
`;

test/no-tags/example/.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../src"
5+
]
6+
]
7+
}
File renamed without changes.

test/no-tags/index.test.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
import { transformFileSync } from '@babel/core'
22
import * as path from 'path'
33

4-
jest.mock('styled-components/package.json', () => ({ version: '4.0.0' }))
4+
let mockVersion
5+
6+
jest.mock('styled-components/package.json', () => ({ version: mockVersion }))
7+
8+
beforeEach(() => jest.resetModules())
9+
10+
it('should not swap out the import if styled-components v3 is detected', () => {
11+
mockVersion = '3.0.0'
12+
13+
const fixturePath = path.join(__dirname + '/example/index.js')
14+
const fixture = transformFileSync(fixturePath).code
15+
16+
expect(fixture).toMatchSnapshot()
17+
})
518

619
it('should swap out the import if styled-components v4+ is detected', () => {
7-
const fixturePath = path.join(__dirname + '/example.js')
20+
mockVersion = '4.0.0'
21+
22+
const fixturePath = path.join(__dirname + '/example/index.js')
23+
const fixture = transformFileSync(fixturePath).code
24+
25+
expect(fixture).toMatchSnapshot()
26+
})
27+
28+
it('should swap out the import if styled-components v4 beta is detected', () => {
29+
mockVersion = '4.0.0-beta.0'
30+
31+
const fixturePath = path.join(__dirname + '/example/index.js')
832
const fixture = transformFileSync(fixturePath).code
933

1034
expect(fixture).toMatchSnapshot()

0 commit comments

Comments
 (0)