1
+ import '@testing-library/jest-dom' ;
2
+ import * as React from 'react' ;
3
+ import { fireEvent , render , screen } from '@testing-library/react' ;
4
+ import { Access } from '../src' ;
5
+ import MyButton from './components/MyButton' ;
6
+
7
+ test ( 'Access Component Not Exist' , ( ) => {
8
+ localStorage . removeItem ( 'authorities' ) ;
9
+
10
+ const mockOnClick = jest . fn ( ) ;
11
+ render (
12
+ < Access hasRoles = { [ "admin" ] } >
13
+ < MyButton text = "Click me" onClick = { mockOnClick } />
14
+ </ Access >
15
+ ) ;
16
+
17
+ // 断言按钮文本
18
+ const button = screen . queryByRole ( 'button' , { name : 'my-button' } ) ;
19
+ // 断言按钮不存在
20
+ expect ( button ) . not . toBeInTheDocument ( ) ;
21
+ } ) ;
22
+
23
+
24
+ test ( 'Access hasRoles Test' , ( ) => {
25
+ const mockOnClick = jest . fn ( ) ;
26
+
27
+ localStorage . setItem ( 'authorities' , JSON . stringify ( [ 'admin' ] ) ) ;
28
+
29
+ render (
30
+ < Access hasRoles = { [ "admin" ] } >
31
+ < MyButton text = "Click me" onClick = { mockOnClick } />
32
+ </ Access >
33
+ ) ;
34
+
35
+ // 断言按钮文本
36
+ const button = screen . getByRole ( 'button' , { name : 'my-button' } ) ;
37
+ expect ( button ) . toBeInTheDocument ( ) ;
38
+
39
+ // 断言按钮点击事件
40
+ fireEvent . click ( button ) ;
41
+ expect ( mockOnClick ) . toHaveBeenCalledTimes ( 1 ) ;
42
+ } ) ;
43
+
44
+
45
+
46
+ test ( 'Access hasAnyRoles Test' , ( ) => {
47
+ const mockOnClick = jest . fn ( ) ;
48
+
49
+ localStorage . setItem ( 'authorities' , JSON . stringify ( [ 'admin' , 'user' ] ) ) ;
50
+
51
+ render (
52
+ < Access hasAnyRoles = { [ "admin" ] } >
53
+ < MyButton text = "Click me" onClick = { mockOnClick } />
54
+ </ Access >
55
+ ) ;
56
+
57
+ // 断言按钮文本
58
+ const button = screen . getByRole ( 'button' , { name : 'my-button' } ) ;
59
+ expect ( button ) . toBeInTheDocument ( ) ;
60
+
61
+ // 断言按钮点击事件
62
+ fireEvent . click ( button ) ;
63
+ expect ( mockOnClick ) . toHaveBeenCalledTimes ( 1 ) ;
64
+ } ) ;
65
+
66
+
67
+ test ( 'Access noRoles Test 1' , ( ) => {
68
+ const mockOnClick = jest . fn ( ) ;
69
+
70
+ localStorage . setItem ( 'authorities' , JSON . stringify ( [ 'admin' ] ) ) ;
71
+
72
+ render (
73
+ < Access noRoles = { [ "user" ] } >
74
+ < MyButton text = "Click me" onClick = { mockOnClick } />
75
+ </ Access >
76
+ ) ;
77
+
78
+ // 断言按钮文本
79
+ const button = screen . getByRole ( 'button' , { name : 'my-button' } ) ;
80
+ expect ( button ) . toBeInTheDocument ( ) ;
81
+
82
+ // 断言按钮点击事件
83
+ fireEvent . click ( button ) ;
84
+ expect ( mockOnClick ) . toHaveBeenCalledTimes ( 1 ) ;
85
+ } ) ;
86
+
87
+ test ( 'Access noRoles Test 2' , ( ) => {
88
+ const mockOnClick = jest . fn ( ) ;
89
+
90
+ localStorage . setItem ( 'authorities' , JSON . stringify ( [ 'admin' ] ) ) ;
91
+
92
+ render (
93
+ < Access noRoles = { [ "admin" ] } >
94
+ < MyButton text = "Click me" onClick = { mockOnClick } />
95
+ </ Access >
96
+ ) ;
97
+
98
+ // 断言按钮文本
99
+ const button = screen . queryByRole ( 'button' , { name : 'my-button' } ) ;
100
+ expect ( button ) . not . toBeInTheDocument ( ) ;
101
+
102
+ } ) ;
103
+
104
+
105
+ test ( 'Access noAnyRoles Test 1' , ( ) => {
106
+ const mockOnClick = jest . fn ( ) ;
107
+
108
+ localStorage . setItem ( 'authorities' , JSON . stringify ( [ 'admin' , 'user' ] ) ) ;
109
+
110
+ render (
111
+ < Access noAnyRoles = { [ "system" ] } >
112
+ < MyButton text = "Click me" onClick = { mockOnClick } />
113
+ </ Access >
114
+ ) ;
115
+
116
+ // 断言按钮文本
117
+ const button = screen . getByRole ( 'button' , { name : 'my-button' } ) ;
118
+ expect ( button ) . toBeInTheDocument ( ) ;
119
+
120
+ // 断言按钮点击事件
121
+ fireEvent . click ( button ) ;
122
+ expect ( mockOnClick ) . toHaveBeenCalledTimes ( 1 ) ;
123
+
124
+ } ) ;
125
+
126
+ test ( 'Access noAnyRoles Test 2' , ( ) => {
127
+ const mockOnClick = jest . fn ( ) ;
128
+
129
+ localStorage . setItem ( 'authorities' , JSON . stringify ( [ 'admin' , 'user' ] ) ) ;
130
+
131
+ render (
132
+ < Access noAnyRoles = { [ "user" ] } >
133
+ < MyButton text = "Click me" onClick = { mockOnClick } />
134
+ </ Access >
135
+ ) ;
136
+
137
+ // 断言按钮文本
138
+ const button = screen . queryByRole ( 'button' , { name : 'my-button' } ) ;
139
+ expect ( button ) . not . toBeInTheDocument ( ) ;
140
+
141
+ } ) ;
142
+
143
+
144
+ test ( 'Access isNotRoles Test 1' , ( ) => {
145
+ const mockOnClick = jest . fn ( ) ;
146
+
147
+ localStorage . setItem ( 'authorities' , JSON . stringify ( [ 'admin' , 'user' ] ) ) ;
148
+
149
+ render (
150
+ < Access isNotRoles = { true } >
151
+ < MyButton text = "Click me" onClick = { mockOnClick } />
152
+ </ Access >
153
+ ) ;
154
+
155
+ // 断言按钮文本
156
+ const button = screen . queryByRole ( 'button' , { name : 'my-button' } ) ;
157
+ expect ( button ) . not . toBeInTheDocument ( ) ;
158
+
159
+ } ) ;
160
+
161
+
162
+ test ( 'Access isNotRoles Test 2' , ( ) => {
163
+ const mockOnClick = jest . fn ( ) ;
164
+
165
+ localStorage . removeItem ( 'authorities' ) ;
166
+
167
+ render (
168
+ < Access isNotRoles = { true } >
169
+ < MyButton text = "Click me" onClick = { mockOnClick } />
170
+ </ Access >
171
+ ) ;
172
+
173
+ // 断言按钮文本
174
+ const button = screen . getByRole ( 'button' , { name : 'my-button' } ) ;
175
+ expect ( button ) . toBeInTheDocument ( ) ;
176
+
177
+ // 断言按钮点击事件
178
+ fireEvent . click ( button ) ;
179
+ expect ( mockOnClick ) . toHaveBeenCalledTimes ( 1 ) ;
180
+
181
+ } ) ;
0 commit comments