Passed
Push — main ( a3dd18...4c1518 )
by Andrii
02:15
created

basic.spec.tsx ➔ Root2   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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