Passed
Push — main ( 953ffa...e37dd6 )
by Andrii
01:54
created

direct.spec.tsx ➔ Root   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
import React from "react"
2
import expectRender from "../expect-to-same-render"
3
import type { ClassNames, ClassNamesProperty, ClassNamed, ClassHash } from "."
4
import classNaming, {classNamesCheck} from "."
5
6
const Button = ({className, "classnames": { Btn }}
7
  : ClassNamed & ClassNamesProperty<{Btn: ClassHash}>
8
) => <button {...classNaming(className, { Btn })}/>
9
10
type RootClassNames = ClassNamesProperty<{
11
  App__Item: ClassHash
12
  App__Footer: ClassHash
13
}>
14
type RootProps = RootClassNames & ClassNames<typeof Button>
15
function Root({classnames, "classnames": { App__Item, App__Footer }}: RootProps) {
16
  const classFooter = classNaming({App__Footer})
17
18
  return <>
19
    <Button {...{
20
      ...classNaming({ App__Item }),
21
      classnames
22
    }}/>
23
    <div {...classFooter} data-classname={`${classFooter}`} />
24
  </>
25
}
26
27
it("not css module", () => expectRender(
28
  <Root classnames={classNamesCheck()}/>
29
).toSame(  
30
  <button className="App__Item Btn"></button>,
31
  <div className="App__Footer" data-classname="App__Footer"></div>
32
))
33
34
it("css module", () => expectRender(
35
  <Root classnames={{
36
    App__Footer: "footer-hash",
37
    App__Item: "item-hash",
38
    Btn: "btn-hash"
39
  }}/>
40
).toSame(
41
  <button className="item-hash btn-hash"></button>,
42
  <div className="footer-hash" data-classname="footer-hash"></div>
43
))
44
45
it("VSCode can rename property", () => {
46
  type RootProps = ClassNamesProperty<{
47
    App__Container: ClassHash
48
  }>
49
50
  function Root({
51
    "classnames": {App__Container: App__Container}
52
  }: RootProps) {
53
    return <div {...classNaming({App: App__Container})}/>
54
  }
55
56
  function Root2({
57
    "classnames": {
58
      // VSCode didn't rename here due to `ClassNames<>` wrapper
59
      //@ts-expect-error Property 'App' does not exist
60
      App: App__Container
61
    }
62
  }: ClassNames<typeof Root>) {
63
    return <div {...classNaming({App: App__Container})}/>
64
  }
65
66
  expectRender(
67
    <Root
68
      classnames={
69
        classNamesCheck()} />,
70
    <Root2 classnames={classNamesCheck()} />
71
  ).toSame(
72
    <div className="App"/>,
73
    <div className="App"/>
74
  )
75
})
76
77
it("additional type check after rename", () => {
78
  type Props1 = ClassNamesProperty<{ class1: ClassHash }>
79
  type Props2 = {
80
    classnames: { class2_renamed: ClassHash }
81
  }
82
  const { class1,
83
    //@ts-expect-error Property 'class2' does not exist 
84
    class2
85
  } = classNamesCheck<Props1 & Props2>();
86
87
  expectRender(
88
    <div {...classNaming<Props1["classnames"]>({class1})} />,
89
    <div {...classNaming<Props2["classnames"]>({
90
      //@ts-expect-error Object literal may only specify known properties, and 'class2' does not exist
91
      class2
92
    })} />
93
  ).toSame(
94
    <div className="class1"/>,
95
    <div className="class2"/>    
96
  )
97
})
98
99
it("chaining", () => {
100
  const props = {className: "Cell", classnames: classNamesCheck()}
101
102
  const {className, "classnames": {
103
    Column_1, Column_2,
104
    Row_1, Row_2
105
  }} = props
106
  , Cell = classNaming(className)
107
  , Col1 = Cell({Column_1})
108
  , Col2 = Cell({Column_2})
109
110
  expectRender(
111
    <div {...Col1({ Row_1 })} />,
112
    <div {...Col1({ Row_2 })} />,
113
    <div {...Col2({ Row_1 })} />,
114
    <div {...Col2({ Row_2 })} />,
115
    <div {...classNaming({ Column_1 })({ Column_2 })({ Row_1 })({ Row_2 })} />
116
  ).toSame(
117
    <div className="Cell Column_1 Row_1" />,
118
    <div className="Cell Column_1 Row_2" />,
119
    <div className="Cell Column_2 Row_1" />,
120
    <div className="Cell Column_2 Row_2" />,
121
    <div className="Column_1 Column_2 Row_1 Row_2" />
122
  )
123
})
124