Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit f30fac0

Browse files
committed
feat: added tests for component selector
1 parent 6f990a1 commit f30fac0

File tree

4 files changed

+130
-8
lines changed

4 files changed

+130
-8
lines changed

packages/styled/src/styled.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export const createStyled: CreateStyled = (tag: any, options?: StyledOptions) =>
6969
const cache = __unusafe_useEmotionCache(defaultCache)
7070
const { as, ...restAttrs } = attrs || {}
7171

72-
7372
let mergedProps = { ...props, ...restAttrs }
7473

7574
let className = attrs.staticClass ? `${attrs.staticClass} ` : ''
@@ -86,6 +85,7 @@ export const createStyled: CreateStyled = (tag: any, options?: StyledOptions) =>
8685
mergedProps[attr] = props[key]
8786
}
8887
}
88+
8989
if (props.theme == null) {
9090
mergedProps.theme = useEmotionTheme(undefined)
9191
}
@@ -102,7 +102,7 @@ export const createStyled: CreateStyled = (tag: any, options?: StyledOptions) =>
102102
clsx(attrs.class as string)
103103
)
104104
}
105-
105+
106106
const serialized = serializeStyles(
107107
styles.concat(classInterpolations),
108108
cache.registered,

packages/styled/tests/__snapshots__/styled.test.tsx.snap

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,32 @@ exports[`styled call expression 1`] = `
7575
</DocumentFragment>
7676
`;
7777

78+
exports[`styled component selectors 1`] = `
79+
<DocumentFragment>
80+
<!---->
81+
.emotion-0 {
82+
color: green;
83+
}
84+
85+
.emotion-0 .emotion-2 {
86+
color: yellow;
87+
}
88+
89+
.emotion-1 {
90+
color: hotpink;
91+
}
92+
93+
<div
94+
class="emotion-0"
95+
>
96+
<!---->
97+
<div
98+
class="emotion-1 emotion-2"
99+
/>
100+
</div>
101+
</DocumentFragment>
102+
`;
103+
78104
exports[`styled composing components 1`] = `
79105
<DocumentFragment>
80106
<!---->
@@ -550,6 +576,56 @@ exports[`styled random object expression 1`] = `
550576
</DocumentFragment>
551577
`;
552578

579+
exports[`styled same component rendered multiple times 1`] = `
580+
<DocumentFragment>
581+
<!---->
582+
.emotion-0 {
583+
color: green;
584+
}
585+
586+
<div
587+
class="emotion-0"
588+
/>
589+
</DocumentFragment>
590+
`;
591+
592+
exports[`styled same component rendered multiple times 2`] = `
593+
<DocumentFragment>
594+
<!---->
595+
.emotion-0 {
596+
color: green;
597+
}
598+
599+
<div
600+
class="emotion-0"
601+
>
602+
<!---->
603+
<div
604+
class="emotion-0"
605+
/>
606+
<!---->
607+
<div
608+
class="emotion-0"
609+
/>
610+
</div>
611+
</DocumentFragment>
612+
`;
613+
614+
exports[`styled theming 1`] = `
615+
<DocumentFragment>
616+
<!---->
617+
.emotion-0 {
618+
color: hotpink;
619+
}
620+
621+
<div
622+
class="emotion-0"
623+
>
624+
this should be hotpink
625+
</div>
626+
</DocumentFragment>
627+
`;
628+
553629
exports[`styled throws if undefined is passed as the component 1`] = `
554630
"You are trying to create a styled element with an undefined component.
555631
You may have forgotten to import it."

packages/styled/tests/styled.test.tsx

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import styled from "../src"
2-
import { h, Fragment } from "vue"
2+
import { h, Fragment, defineComponent } from "vue"
33
import { render } from "../../test-utils/src"
44
import { createSerializer } from "@emotion/jest"
55
import { css } from '../src/styled'
@@ -530,21 +530,21 @@ describe.only("styled", () => {
530530
expect(asFragment()).toMatchSnapshot()
531531
})
532532

533-
it.todo("Finalize unit tests for theme provider from the Styled Factory Function")
534-
it.skip('theming', () => {
533+
it('theming', () => {
535534
const Div = styled.div`
536535
color: ${props => props.theme.primary};
537536
`
538537

539-
const { asFragment } = render(() => {
538+
const { asFragment } = render(defineComponent(() => {
540539
EmotionThemeProvider({
541540
primary: 'hotpink'
542541
})
543542

544-
return (
543+
return () => (
545544
<Div>this should be hotpink</Div>
546545
)
547546
})
547+
)
548548

549549
expect(asFragment()).toMatchSnapshot()
550550
})
@@ -565,4 +565,50 @@ describe.only("styled", () => {
565565

566566
expect(asFragment()).toMatchSnapshot()
567567
})
568+
569+
it("same component rendered multiple times", () => {
570+
const SomeComponent = styled.div`
571+
color: green;
572+
`
573+
const { asFragment } = render(() => (
574+
<SomeComponent />
575+
))
576+
const tree = asFragment()
577+
expect(tree).toMatchSnapshot()
578+
expect(render(() => (
579+
<SomeComponent />
580+
)).asFragment()).toEqual(render(() => (
581+
<SomeComponent />
582+
)).asFragment())
583+
584+
expect(render(() => (
585+
<SomeComponent>
586+
<SomeComponent />
587+
<SomeComponent />
588+
</SomeComponent>
589+
)).asFragment()).toMatchSnapshot()
590+
})
591+
592+
it("component selectors", () => {
593+
let Target = styled('div', {
594+
// if anyone is looking this
595+
// please don't do this.
596+
// use the babel plugin/macro.
597+
target: 'e322f2d3tbrgf2e0'
598+
})`
599+
color: hotpink;
600+
`
601+
602+
let SomeComponent = styled.div`
603+
color: green;
604+
${Target.toString()} {
605+
color: yellow;
606+
}
607+
`
608+
expect(render(() => (
609+
<SomeComponent>
610+
<Target />
611+
</SomeComponent>
612+
)).asFragment()).toMatchSnapshot()
613+
})
568614
})

packages/test-utils/src/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const render = (
3636
name: "ChakraUIVueTestContainer",
3737
setup(_, { slots }) {
3838
useDefaultProviders()
39-
return () => h(component as any, slots)
39+
return () => h(component as any, {}, slots)
4040
},
4141
}),
4242
...rest

0 commit comments

Comments
 (0)