Skip to content

Commit beec960

Browse files
committed
Fix: Create custom JSS instance
To prevent manipulations on the default instance (like plugins setup) affecting Styleguidist styles.
1 parent 286431f commit beec960

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/styles/__tests__/setupjss.spec.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import jssBase from 'jss';
2+
import jss from '../setupjss';
3+
4+
describe('setupjss', () => {
5+
it('should renerate prefixed class names', () => {
6+
const { classes } = jss.createStyleSheet({
7+
root: {},
8+
});
9+
expect(classes.root).toMatch(/^rsg--\w+-\d+$/);
10+
});
11+
12+
it('jss-global plugin should be enabled', () => {
13+
const css = jss
14+
.createStyleSheet({
15+
'@global body': {
16+
color: 'red',
17+
},
18+
})
19+
.toString();
20+
expect(css).toMatch(/^body {/);
21+
});
22+
23+
it('jss plugins should be enabled', () => {
24+
const stylesheet = jss.createStyleSheet({
25+
root: {
26+
backgroundColor: 'tomato',
27+
width: 1,
28+
'&:hover': {
29+
color: 'snow',
30+
},
31+
},
32+
child: {
33+
composes: '$root',
34+
color: 'blue',
35+
},
36+
});
37+
38+
const root = stylesheet.getRule('root').style;
39+
expect(root).toEqual(expect.any(Object));
40+
expect(root['background-color']).toBe('tomato');
41+
expect(root.width).toBe('1px');
42+
expect(stylesheet.classes.root).toMatch(/^rsg--root-\d+$/);
43+
44+
const child = stylesheet.getRule('child').style;
45+
expect(child).toEqual(expect.any(Object));
46+
expect(child.color).toBe('blue');
47+
expect(stylesheet.classes.child).toMatch(/^rsg--child-\d+ rsg--root-\d+$/);
48+
49+
const hover = stylesheet.rules.map['.rsg--root-2:hover'];
50+
expect(hover).toEqual(expect.any(Object));
51+
expect(hover.style.color).toBe('snow');
52+
});
53+
54+
it('base jss instance setup shoud not affect Styleguidist styles', () => {
55+
jssBase.setup();
56+
57+
const stylesheet = jss.createStyleSheet({
58+
root: {
59+
width: 1,
60+
},
61+
});
62+
63+
expect(stylesheet.classes.root).toMatch(/^rsg--root-\d+$/);
64+
65+
const root = stylesheet.getRule('root').style;
66+
expect(root.width).toBe('1px');
67+
});
68+
});

src/styles/setupjss.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import jss from 'jss';
1+
import { create } from 'jss';
22
import global from 'jss-global';
33
import isolate from 'jss-isolate';
44
import nested from 'jss-nested';
@@ -12,7 +12,7 @@ const createGenerateClassName = () => {
1212
return rule => `rsg--${rule.key}-${counter++}`;
1313
};
1414

15-
jss.setup({
15+
const jss = create({
1616
createGenerateClassName,
1717
plugins: [
1818
global(),

0 commit comments

Comments
 (0)