|
1 |
| -import { setFileScope, endFileScope } from './fileScope'; |
2 |
| -import { createVar } from './vars'; |
3 |
| -import { transformCss } from './transformCss'; |
| 1 | +import { endFileScope, setFileScope } from './fileScope'; |
4 | 2 | import { style } from './style';
|
| 3 | +import { transformCss } from './transformCss'; |
| 4 | +import { createVar } from './vars'; |
5 | 5 |
|
6 | 6 | setFileScope('test');
|
7 | 7 |
|
@@ -2500,6 +2500,285 @@ describe('transformCss', () => {
|
2500 | 2500 | }
|
2501 | 2501 | `);
|
2502 | 2502 | });
|
| 2503 | + |
| 2504 | + it('should handle @position-try declaration', () => { |
| 2505 | + expect( |
| 2506 | + transformCss({ |
| 2507 | + composedClassLists: [], |
| 2508 | + localClassNames: ['testClass'], |
| 2509 | + cssObjs: [ |
| 2510 | + { |
| 2511 | + type: 'local', |
| 2512 | + selector: 'testClass', |
| 2513 | + rule: { |
| 2514 | + display: 'flex', |
| 2515 | + '@position-try': { |
| 2516 | + '--custom-left': { |
| 2517 | + width: '100px', |
| 2518 | + margin: '0 10px 0 0', |
| 2519 | + }, |
| 2520 | + }, |
| 2521 | + }, |
| 2522 | + }, |
| 2523 | + ], |
| 2524 | + }).join('\n'), |
| 2525 | + ).toMatchInlineSnapshot(` |
| 2526 | + .testClass { |
| 2527 | + display: flex; |
| 2528 | + } |
| 2529 | + @position-try --custom-left { |
| 2530 | + .testClass { |
| 2531 | + width: 100px; |
| 2532 | + margin: 0 10px 0 0; |
| 2533 | + } |
| 2534 | + } |
| 2535 | + `); |
| 2536 | + }); |
| 2537 | + |
| 2538 | + it('should multiple custom position inside @position-try declaration', () => { |
| 2539 | + expect( |
| 2540 | + transformCss({ |
| 2541 | + composedClassLists: [], |
| 2542 | + localClassNames: ['testClass'], |
| 2543 | + cssObjs: [ |
| 2544 | + { |
| 2545 | + type: 'local', |
| 2546 | + selector: 'testClass', |
| 2547 | + rule: { |
| 2548 | + display: 'flex', |
| 2549 | + '@position-try': { |
| 2550 | + '--custom-left': { |
| 2551 | + width: '100px', |
| 2552 | + margin: '0 10px 0 0', |
| 2553 | + }, |
| 2554 | + '--custom-right': { |
| 2555 | + width: '100px', |
| 2556 | + margin: '0 10px 0 0', |
| 2557 | + }, |
| 2558 | + }, |
| 2559 | + }, |
| 2560 | + }, |
| 2561 | + ], |
| 2562 | + }).join('\n'), |
| 2563 | + ).toMatchInlineSnapshot(` |
| 2564 | + .testClass { |
| 2565 | + display: flex; |
| 2566 | + } |
| 2567 | + @position-try --custom-left { |
| 2568 | + .testClass { |
| 2569 | + width: 100px; |
| 2570 | + margin: 0 10px 0 0; |
| 2571 | + } |
| 2572 | + } |
| 2573 | + @position-try --custom-right { |
| 2574 | + .testClass { |
| 2575 | + width: 100px; |
| 2576 | + margin: 0 10px 0 0; |
| 2577 | + } |
| 2578 | + } |
| 2579 | + `); |
| 2580 | + }); |
| 2581 | + |
| 2582 | + it('should handle @position-try inside media queries', () => { |
| 2583 | + expect( |
| 2584 | + transformCss({ |
| 2585 | + composedClassLists: [], |
| 2586 | + localClassNames: ['testClass'], |
| 2587 | + cssObjs: [ |
| 2588 | + { |
| 2589 | + type: 'local', |
| 2590 | + selector: 'testClass', |
| 2591 | + rule: { |
| 2592 | + display: 'flex', |
| 2593 | + '@media': { |
| 2594 | + 'screen and (min-width: 700px)': { |
| 2595 | + '@position-try': { |
| 2596 | + '--custom-left': { |
| 2597 | + width: '100px', |
| 2598 | + margin: '0 10px 0 0', |
| 2599 | + }, |
| 2600 | + }, |
| 2601 | + }, |
| 2602 | + }, |
| 2603 | + }, |
| 2604 | + }, |
| 2605 | + ], |
| 2606 | + }).join('\n'), |
| 2607 | + ).toMatchInlineSnapshot(` |
| 2608 | + .testClass { |
| 2609 | + display: flex; |
| 2610 | + } |
| 2611 | + @media screen and (min-width: 700px) { |
| 2612 | + @position-try --custom-left { |
| 2613 | + .testClass { |
| 2614 | + width: 100px; |
| 2615 | + margin: 0 10px 0 0; |
| 2616 | + } |
| 2617 | + } |
| 2618 | + } |
| 2619 | + `); |
| 2620 | + }); |
| 2621 | + |
| 2622 | + it('should handle @position-try inside container queries', () => { |
| 2623 | + expect( |
| 2624 | + transformCss({ |
| 2625 | + composedClassLists: [], |
| 2626 | + localClassNames: ['testClass'], |
| 2627 | + cssObjs: [ |
| 2628 | + { |
| 2629 | + type: 'local', |
| 2630 | + selector: 'testClass', |
| 2631 | + rule: { |
| 2632 | + display: 'flex', |
| 2633 | + '@container': { |
| 2634 | + 'sidebar (min-width: 700px)': { |
| 2635 | + '@position-try': { |
| 2636 | + '--custom-left': { |
| 2637 | + width: '100px', |
| 2638 | + margin: '0 10px 0 0', |
| 2639 | + }, |
| 2640 | + }, |
| 2641 | + }, |
| 2642 | + }, |
| 2643 | + }, |
| 2644 | + }, |
| 2645 | + ], |
| 2646 | + }).join('\n'), |
| 2647 | + ).toMatchInlineSnapshot(` |
| 2648 | + .testClass { |
| 2649 | + display: flex; |
| 2650 | + } |
| 2651 | + @container sidebar (min-width: 700px) { |
| 2652 | + @position-try --custom-left { |
| 2653 | + .testClass { |
| 2654 | + width: 100px; |
| 2655 | + margin: 0 10px 0 0; |
| 2656 | + } |
| 2657 | + } |
| 2658 | + } |
| 2659 | + `); |
| 2660 | + }); |
| 2661 | + |
| 2662 | + it('should handle @position-try inside a layer', () => { |
| 2663 | + expect( |
| 2664 | + transformCss({ |
| 2665 | + composedClassLists: [], |
| 2666 | + localClassNames: ['testClass'], |
| 2667 | + cssObjs: [ |
| 2668 | + { |
| 2669 | + type: 'local', |
| 2670 | + selector: 'testClass', |
| 2671 | + rule: { |
| 2672 | + '@layer': { |
| 2673 | + 'mock-layer': { |
| 2674 | + '@position-try': { |
| 2675 | + '--custom-left': { |
| 2676 | + width: '100px', |
| 2677 | + margin: '0 10px 0 0', |
| 2678 | + }, |
| 2679 | + }, |
| 2680 | + }, |
| 2681 | + }, |
| 2682 | + }, |
| 2683 | + }, |
| 2684 | + ], |
| 2685 | + }).join('\n'), |
| 2686 | + ).toMatchInlineSnapshot(` |
| 2687 | + @layer mock-layer; |
| 2688 | + @layer mock-layer { |
| 2689 | + @position-try --custom-left { |
| 2690 | + .testClass { |
| 2691 | + width: 100px; |
| 2692 | + margin: 0 10px 0 0; |
| 2693 | + } |
| 2694 | + } |
| 2695 | + } |
| 2696 | + `); |
| 2697 | + }); |
| 2698 | + |
| 2699 | + it('should throw an error when not using a <dashed-ident> type in @position-try scope', () => { |
| 2700 | + expect(() => |
| 2701 | + transformCss({ |
| 2702 | + composedClassLists: [], |
| 2703 | + localClassNames: ['testClass'], |
| 2704 | + cssObjs: [ |
| 2705 | + { |
| 2706 | + type: 'local', |
| 2707 | + selector: 'testClass', |
| 2708 | + rule: { |
| 2709 | + display: 'flex', |
| 2710 | + '@position-try': { |
| 2711 | + // @ts-expect-error This test is to cover the error for non-allowed properties inside @position-try scope |
| 2712 | + invalidName: { |
| 2713 | + backgroundColor: 'blue', |
| 2714 | + }, |
| 2715 | + }, |
| 2716 | + }, |
| 2717 | + }, |
| 2718 | + ], |
| 2719 | + }), |
| 2720 | + ).toThrowErrorMatchingInlineSnapshot( |
| 2721 | + 'Invalid @position-try name: "invalidName". Position names must follow the <dashed-ident> type (--custom-name).', |
| 2722 | + ); |
| 2723 | + }); |
| 2724 | + |
| 2725 | + it('should throw an error when using a non allowed property inside @position-try scope', () => { |
| 2726 | + expect(() => |
| 2727 | + transformCss({ |
| 2728 | + composedClassLists: [], |
| 2729 | + localClassNames: ['testClass'], |
| 2730 | + cssObjs: [ |
| 2731 | + { |
| 2732 | + type: 'local', |
| 2733 | + selector: 'testClass', |
| 2734 | + rule: { |
| 2735 | + display: 'flex', |
| 2736 | + '@position-try': { |
| 2737 | + '--custom-left': { |
| 2738 | + // @ts-expect-error This test is to cover the error for non-allowed properties inside @position-try scope |
| 2739 | + backgroundColor: 'blue', |
| 2740 | + }, |
| 2741 | + }, |
| 2742 | + }, |
| 2743 | + }, |
| 2744 | + ], |
| 2745 | + }), |
| 2746 | + ).toThrowErrorMatchingInlineSnapshot( |
| 2747 | + 'Invalid properties in @position-try --custom-left rule: backgroundColor. Only inset, margin, sizing, self-alignment, position-anchor, and position-area properties are allowed.', |
| 2748 | + ); |
| 2749 | + }); |
| 2750 | + |
| 2751 | + it('should throw an error when a at-rule is use inside @position-try scope', () => { |
| 2752 | + expect(() => |
| 2753 | + transformCss({ |
| 2754 | + composedClassLists: [], |
| 2755 | + localClassNames: ['testClass'], |
| 2756 | + cssObjs: [ |
| 2757 | + { |
| 2758 | + type: 'local', |
| 2759 | + selector: 'testClass', |
| 2760 | + rule: { |
| 2761 | + display: 'flex', |
| 2762 | + '@position-try': { |
| 2763 | + '--custom-left': { |
| 2764 | + width: '100px', |
| 2765 | + margin: '0 10px 0 0', |
| 2766 | + // @ts-expect-error This test is to cover the error when a at-rule is used inside @position-try scope |
| 2767 | + '@media': { |
| 2768 | + 'screen and (min-width: 700px)': { |
| 2769 | + display: 'grid', |
| 2770 | + }, |
| 2771 | + }, |
| 2772 | + }, |
| 2773 | + }, |
| 2774 | + }, |
| 2775 | + }, |
| 2776 | + ], |
| 2777 | + }), |
| 2778 | + ).toThrowErrorMatchingInlineSnapshot( |
| 2779 | + 'Nested at-rules (e.g. "@media") are not allowed inside @position-try block.', |
| 2780 | + ); |
| 2781 | + }); |
2503 | 2782 | });
|
2504 | 2783 |
|
2505 | 2784 | endFileScope();
|
0 commit comments