|
1 | 1 | import { vi } from 'vitest';
|
2 | 2 | import { renderHook } from '@testing-library/react-hooks/native';
|
3 | 3 | import { useContext } from 'react';
|
4 |
| -import useVariant from './useVariant'; |
| 4 | +import useVariant, { variantHasChanged } from './useVariant'; |
5 | 5 |
|
6 | 6 | vi.mock('react', async () => {
|
7 | 7 | const react = (await vi.importActual('react')) as any;
|
@@ -158,3 +158,87 @@ test('should remove event listeners when unmounted', () => {
|
158 | 158 | expect(clientMock.off).nthCalledWith(1, ...clientMock.on.mock.calls[0]);
|
159 | 159 | expect(clientMock.off).nthCalledWith(2, ...clientMock.on.mock.calls[1]);
|
160 | 160 | });
|
| 161 | + |
| 162 | +describe('Variant change detection', () => { |
| 163 | + test('If the variants are identical, it returns `false`', () => { |
| 164 | + const a = { |
| 165 | + name: 'a', |
| 166 | + enabled: true, |
| 167 | + payload: { |
| 168 | + type: 'string', |
| 169 | + value: 'data', |
| 170 | + }, |
| 171 | + }; |
| 172 | + const b = { |
| 173 | + name: 'a', |
| 174 | + enabled: true, |
| 175 | + payload: { |
| 176 | + type: 'string', |
| 177 | + value: 'data', |
| 178 | + }, |
| 179 | + }; |
| 180 | + |
| 181 | + expect(variantHasChanged(a, b)).toBeFalsy(); |
| 182 | + }); |
| 183 | + |
| 184 | + test('If the new variant is undefined, it counts as a change', () => { |
| 185 | + const a = { name: 'a', enabled: true }; |
| 186 | + |
| 187 | + expect(variantHasChanged(a, undefined)).toBeTruthy(); |
| 188 | + }); |
| 189 | + |
| 190 | + test('Name change is detected', () => { |
| 191 | + const a = { name: 'a', enabled: true }; |
| 192 | + const b = { name: 'b', enabled: true }; |
| 193 | + |
| 194 | + expect(variantHasChanged(a, b)).toBeTruthy(); |
| 195 | + }); |
| 196 | + |
| 197 | + test('Enabled state change is detected', () => { |
| 198 | + const enabled = { name: 'a', enabled: true }; |
| 199 | + const disabled = { name: 'a', enabled: false }; |
| 200 | + |
| 201 | + expect(variantHasChanged(enabled, disabled)).toBeTruthy(); |
| 202 | + }); |
| 203 | + test('Payload type change is detected', () => { |
| 204 | + const a = { |
| 205 | + name: 'a', |
| 206 | + enabled: true, |
| 207 | + payload: { |
| 208 | + type: 'string', |
| 209 | + value: '{}', |
| 210 | + }, |
| 211 | + }; |
| 212 | + const b = { |
| 213 | + name: 'a', |
| 214 | + enabled: true, |
| 215 | + payload: { |
| 216 | + type: 'json', |
| 217 | + value: '{}', |
| 218 | + }, |
| 219 | + }; |
| 220 | + |
| 221 | + expect(variantHasChanged(a, b)).toBeTruthy(); |
| 222 | + }); |
| 223 | + |
| 224 | + test('Payload value change is detected', () => { |
| 225 | + const a = { |
| 226 | + name: 'a', |
| 227 | + enabled: true, |
| 228 | + payload: { |
| 229 | + type: 'string', |
| 230 | + value: '1', |
| 231 | + }, |
| 232 | + }; |
| 233 | + const b = { |
| 234 | + name: 'a', |
| 235 | + enabled: true, |
| 236 | + payload: { |
| 237 | + type: 'string', |
| 238 | + value: '2', |
| 239 | + }, |
| 240 | + }; |
| 241 | + |
| 242 | + expect(variantHasChanged(a, b)).toBeTruthy(); |
| 243 | + }); |
| 244 | +}); |
0 commit comments