|
| 1 | +import React from 'react'; |
| 2 | +import { View } from 'react-native'; |
1 | 3 | import { describe, it, expect } from 'vitest';
|
| 4 | +import { render } from '@testing-library/react-native'; |
2 | 5 | import { defineTokens } from './';
|
3 | 6 |
|
4 | 7 | describe('sva', () => {
|
@@ -348,3 +351,85 @@ describe('defineTokens', () => {
|
348 | 351 | });
|
349 | 352 | });
|
350 | 353 | });
|
| 354 | + |
| 355 | +describe('styled', () => { |
| 356 | + it('should render a styled component', async () => { |
| 357 | + const { styled } = defineTokens({}); |
| 358 | + const StyledView = styled(View)({}); |
| 359 | + |
| 360 | + const rendered = render(React.createElement(StyledView, { testID: 'styled-view' })); |
| 361 | + const view = await rendered.getByTestId('styled-view'); |
| 362 | + |
| 363 | + expect(view.props.style).toMatchObject({}); |
| 364 | + }); |
| 365 | + |
| 366 | + it('should use defined tokens from the styled function', async () => { |
| 367 | + const { styled } = defineTokens({ |
| 368 | + colors: { |
| 369 | + primary: 'red', |
| 370 | + secondary: 'blue', |
| 371 | + }, |
| 372 | + space: { |
| 373 | + small: 10, |
| 374 | + medium: 20, |
| 375 | + large: 30, |
| 376 | + }, |
| 377 | + }); |
| 378 | + |
| 379 | + const StyledView = styled(View)({ |
| 380 | + backgroundColor: '$colors.primary', |
| 381 | + padding: '$space.small', |
| 382 | + }); |
| 383 | + |
| 384 | + const rendered = render(React.createElement(StyledView, { testID: 'styled-view' })); |
| 385 | + const view = await rendered.getByTestId('styled-view'); |
| 386 | + |
| 387 | + expect(view.props.style).toMatchObject({ |
| 388 | + backgroundColor: 'red', |
| 389 | + padding: 10, |
| 390 | + }); |
| 391 | + }); |
| 392 | + |
| 393 | + it('should merge styles from props and tokens', async () => { |
| 394 | + const { styled } = defineTokens({ |
| 395 | + colors: { |
| 396 | + primary: 'red', |
| 397 | + }, |
| 398 | + }); |
| 399 | + |
| 400 | + const StyledView = styled(View)({ |
| 401 | + backgroundColor: '$colors.primary', |
| 402 | + }); |
| 403 | + |
| 404 | + const rendered = render( |
| 405 | + React.createElement(StyledView, { |
| 406 | + testID: 'styled-view', |
| 407 | + style: { backgroundColor: 'blue' }, |
| 408 | + }), |
| 409 | + ); |
| 410 | + const view = await rendered.getByTestId('styled-view'); |
| 411 | + |
| 412 | + expect(view.props.style).toMatchObject([ |
| 413 | + { |
| 414 | + backgroundColor: 'red', |
| 415 | + }, |
| 416 | + { backgroundColor: 'blue' }, |
| 417 | + ]); |
| 418 | + }); |
| 419 | + |
| 420 | + it('should ignore non defined tokens', async () => { |
| 421 | + const { styled } = defineTokens({}); |
| 422 | + const StyledView = styled(View)({ |
| 423 | + backgroundColor: '$colors.primary', |
| 424 | + }); |
| 425 | + |
| 426 | + const rendered = render( |
| 427 | + React.createElement(StyledView, { |
| 428 | + testID: 'styled-view', |
| 429 | + }), |
| 430 | + ); |
| 431 | + const view = await rendered.getByTestId('styled-view'); |
| 432 | + |
| 433 | + expect(view.props.style).toMatchObject({}); |
| 434 | + }); |
| 435 | +}); |
0 commit comments