Passed
Push — main ( eb8b76...ea2980 )
by Andrii
02:38
created

main.types.test.ts ➔ Functional   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
1
import {
2
  Component,
3
  PureComponent
4
} from "react"
5
import type {
6
  ClassHash,
7
  ClassNamesProperty,
8
  ClassNames,
9
  ClassNamed
10
} from "./main.types"
11
12
type Props = ClassNames<true, ClassNamesProperty<{
13
  prop1: ClassHash
14
  prop2: ClassHash
15
}>>
16
function Functional(_: ClassNamed & ClassNames<ClassNamesProperty<{
17
  func1: ClassHash
18
  func2: ClassHash
19
}>>) { return null }
20
class ClassComponent extends Component<{isValid: boolean} & ClassNames<ClassNamesProperty<{
21
  comp1: ClassHash
22
  comp2: ClassHash
23
}>>> {}
24
class ClassPureComponent extends PureComponent<ClassNames<ClassNamesProperty<{
25
  pure1: ClassHash
26
  pure2: ClassHash
27
}>>> {}
28
29
describe("ClassNames", () => {
30
  describe("direct", () => {
31
    it("<true>", () => {
32
      const suites: Record<string, ClassNames<true>> = {
33
        "className only": {className: ""},
34
        //@ts-expect-error Property 'className' is missing
35
        "empty object"
36
        : {},
37
        "classnames only": {
38
          //@ts-expect-error Object literal may only specify known properties, but 'classnames' does not exist
39
          classnames: {}
40
        },
41
        "className and classnames": {
42
          className: "",
43
          //@ts-expect-error Object literal may only specify known properties, but 'classnames' does not exist
44
          classnames: {}
45
        }
46
      }
47
      expect(suites).toBeInstanceOf(Object)
48
    })
49
  
50
    it("<{class1, class2}>", () => {
51
      const suites: Record<string, ClassNames<{classnames: {class1: ClassHash; class2: ClassHash}}>> = {
52
        "omitted": {
53
          //@ts-expect-error
54
          classnames: {
55
            class1: undefined
56
          }
57
        },
58
        "classnames only": {
59
          classnames: {class1: undefined, class2: undefined}
60
        },
61
        "className only": {
62
          //@ts-expect-error Object literal may only specify known properties, but 'className' does not exist
63
          className: ""
64
        },
65
        //@ts-expect-error Property 'classnames' is missing
66
        "empty object"
67
        : {},
68
        "className and classnames": {
69
          //@ts-expect-error Object literal may only specify known properties, but 'className' does not exist
70
          className: "",
71
          classnames: {class1: undefined, class2: undefined}
72
        }
73
      }
74
      expect(suites).toBeInstanceOf(Object)
75
    })
76
  
77
    it("<true, {class1, class2}>", () => {
78
      const suites: Record<string, ClassNames<true, {classnames: {class1: ClassHash; class2: ClassHash}}>> = {
79
        "className and classnames": {
80
          className: "",
81
          classnames: {class1: undefined, class2: undefined}
82
        },
83
        //@ts-expect-error Property 'className' is missing
84
        "classnames only": {
85
          classnames: {class1: undefined, class2: undefined}
86
        },
87
        //@ts-expect-error Property 'classnames' is missing
88
        "className only": {
89
          className: ""
90
        }
91
      }
92
      expect(suites).toBeInstanceOf(Object)
93
    })
94
  
95
    it("nothing to pick", () => {
96
      type NoClassNames = ClassNames<true>
97
      const suite1: Record<string, ClassNames<
98
        //@ts-expect-error
99
        NoClassNames,
100
        {classnames: {class1: ClassHash}},
101
        {classnames: {class1: ClassHash}}
102
      >> = {
103
        "nothing": {classnames: {class1: ""}}
104
      }
105
      const suite2: Record<string, ClassNames<
106
        {classnames: {class1: ClassHash}},
107
        {classnames: {class1: ClassHash}},
108
        //@ts-expect-error
109
        NoClassNames
110
      >> = {
111
        "nothing": {classnames: {class1: ""}}
112
      }
113
      expect({suite1, suite2}).toBeInstanceOf(Object)
114
    })
115
  })
116
117
  describe("from", () => {  
118
    it("multiple apply", () => {
119
      type AppClassNames = ClassNames<
120
          true,
121
          ClassNamesProperty<{App: ClassHash}>,
122
          typeof ClassComponent,
123
          typeof ClassPureComponent,
124
          typeof Functional,
125
          Props
126
      >;
127
  
128
      const suites: Record<string, AppClassNames["classnames"]> = {
129
        "exact": {
130
          App: undefined,
131
          comp1: undefined,
132
          comp2: undefined,
133
          func1: undefined,
134
          func2: undefined,
135
          prop1: undefined,
136
          prop2: undefined,
137
          pure1: undefined,
138
          pure2: undefined
139
        },
140
        "redundant": {
141
          App: undefined,
142
          comp1: undefined,
143
          comp2: undefined,
144
          func1: undefined,
145
          func2: undefined,
146
          prop1: undefined,
147
          prop2: undefined,
148
          pure1: undefined,
149
          pure2: undefined,
150
          //@ts-expect-error Object literal may only specify known properties, and 'redundant' does not exist
151
          redundant: undefined,
152
        },
153
        //@ts-expect-error Property 'App' is missing
154
        "missed App": {
155
          comp1: undefined,
156
          comp2: undefined,
157
          func1: undefined,
158
          func2: undefined,
159
          prop1: undefined,
160
          prop2: undefined,
161
          pure1: undefined,
162
          pure2: undefined,
163
        },
164
        //@ts-expect-error Property 'component' is missing
165
        "missed comp2": {
166
          App: undefined,
167
          comp1: undefined,
168
          func1: undefined,
169
          func2: undefined,
170
          prop1: undefined,
171
          prop2: undefined,
172
          pure1: undefined,
173
          pure2: undefined,
174
        },
175
        //@ts-expect-error Property 'pureComponent' is missing
176
        "missed pure2": {
177
          App: undefined,
178
          comp1: undefined,
179
          comp2: undefined,
180
          func1: undefined,
181
          func2: undefined,
182
          prop1: undefined,
183
          prop2: undefined,
184
          pure1: undefined,
185
        },
186
        //@ts-expect-error Property 'functional' is missing 
187
        "missed func2": {
188
          App: undefined,
189
          comp1: undefined,
190
          comp2: undefined,
191
          func1: undefined,
192
          prop1: undefined,
193
          prop2: undefined,
194
          pure1: undefined,
195
          pure2: undefined,
196
        },
197
        //@ts-expect-error Property 'props' is missing
198
        "missed prop2": {
199
          App: undefined,
200
          comp1: undefined,
201
          comp2: undefined,
202
          func1: undefined,
203
          func2: undefined,
204
          prop1: undefined,
205
          pure1: undefined,
206
          pure2: undefined,
207
        }        
208
      }
209
  
210
      expect(suites).toBeInstanceOf(Object)
211
    })
212
  })    
213
})
214
215
describe("ClassNamesProperty", () => {
216
  it("Free declaration", () => {
217
    type Props = ClassNamesProperty<{
218
      class1: ClassHash
219
      class2: ClassHash
220
    }>
221
    const suites: Record<string, Props["classnames"]> = {
222
      "all setted": {
223
        class1: "class1",
224
        class2: undefined
225
      },
226
      "redundant": {
227
        class1: "class1",
228
        class2: undefined,
229
        //@ts-expect-error
230
        redundant: "redundant"
231
      },
232
      //@ts-expect-error
233
      "missed": {
234
        class1: "class1"
235
      },
236
      "wrong type": {
237
        class1: "class1",
238
        //@ts-expect-error
239
        class2: false
240
      }
241
    }
242
    expect(suites).toBeInstanceOf(Object)
243
  })
244
  
245
  it("Module based", () => {
246
    type CssModule = {
247
      App: ClassHash
248
      class1: ClassHash, class2: ClassHash
249
    }
250
251
    type Props = ClassNamesProperty<CssModule, {
252
      class1: ClassHash
253
      class2: ClassHash
254
      //TODO #12 Why no suggestion? Means - no rename effect
255
    }>
256
257
    type PropsWithWrong = ClassNamesProperty<CssModule,
258
      //@ts-expect-error
259
      {
260
        class1: ClassHash
261
        class3: ClassHash
262
      }
263
    >
264
    
265
    const suite4wrong: PropsWithWrong["classnames"] = {
266
        //@ts-expect-error Object literal may only specify known properties, but 'class3' does not exist
267
        class3: undefined,
268
    },
269
    suite: Props["classnames"] = {
270
      class1: "class1",
271
      class2: undefined
272
    }
273
    expect({suite4wrong, suite}).toBeInstanceOf(Object)
274
  })
275
})
276