|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
1 | 2 | import { getCompilerContext } from '@/context' |
2 | 3 | import { replaceWxml } from '@/wxml' |
3 | 4 | import { generateCode, isPropsMatch } from '@/wxml/utils' |
4 | 5 | import { removeWxmlId } from '../util' |
5 | 6 |
|
| 7 | +const EXACT_RD_BTN_CLASS_REGEXP = /^rd-btn-class$/ |
| 8 | +const RD_WORD_CLASS_REGEXP = /^rd-\w+-class$/ |
| 9 | +const RD_ALPHA_CLASS_REGEXP = /^rd-[A-Za-z]+-class$/ |
| 10 | + |
6 | 11 | describe('utils', () => { |
7 | 12 | it('isPropsMatch', () => { |
8 | 13 | expect(isPropsMatch('a', 'a')).toBe(true) |
9 | 14 | expect(isPropsMatch(['a'], 'a')).toBe(true) |
10 | 15 | expect(isPropsMatch('rd-btn-class', 'rd-btn-class')).toBe(true) |
11 | 16 | expect(isPropsMatch(['rd-btn-class'], 'rd-btn-class')).toBe(true) |
12 | | - expect(isPropsMatch(/^rd-btn-class$/, 'rd-btn-class')).toBe(true) |
13 | | - expect(isPropsMatch([/^rd-btn-class$/], 'rd-btn-class')).toBe(true) |
14 | | - expect(isPropsMatch(/^rd-\w+-class$/, 'rd-btn-class')).toBe(true) |
15 | | - expect(isPropsMatch([/^rd-\w+-class$/], 'rd-btn-class')).toBe(true) |
16 | | - expect(isPropsMatch(/^rd-[A-Za-z]+-class$/, 'rd-btn-class')).toBe(true) |
17 | | - expect(isPropsMatch([/^rd-[A-Za-z]+-class$/], 'rd-btn-class')).toBe(true) |
| 17 | + expect(isPropsMatch(EXACT_RD_BTN_CLASS_REGEXP, 'rd-btn-class')).toBe(true) |
| 18 | + expect(isPropsMatch([EXACT_RD_BTN_CLASS_REGEXP], 'rd-btn-class')).toBe(true) |
| 19 | + expect(isPropsMatch(RD_WORD_CLASS_REGEXP, 'rd-btn-class')).toBe(true) |
| 20 | + expect(isPropsMatch([RD_WORD_CLASS_REGEXP], 'rd-btn-class')).toBe(true) |
| 21 | + expect(isPropsMatch(RD_ALPHA_CLASS_REGEXP, 'rd-btn-class')).toBe(true) |
| 22 | + expect(isPropsMatch([RD_ALPHA_CLASS_REGEXP], 'rd-btn-class')).toBe(true) |
18 | 23 | }) |
19 | 24 |
|
20 | 25 | it('remove all id', () => { |
@@ -54,4 +59,47 @@ describe('utils', () => { |
54 | 59 | }) |
55 | 60 | expect(code).toContain(replaceWxml('border-[#ff0000] bg-blue-600/50')) |
56 | 61 | }) |
| 62 | + |
| 63 | + it('does not retry when wrapExpression is already enabled', () => { |
| 64 | + const jsHandler = vi.fn(() => ({ |
| 65 | + code: 'wrapped', |
| 66 | + error: new Error('already wrapped'), |
| 67 | + })) |
| 68 | + const runtimeSet = new Set<string>() |
| 69 | + const code = generateCode(`{'foo': flag}`, { |
| 70 | + jsHandler, |
| 71 | + runtimeSet, |
| 72 | + wrapExpression: true, |
| 73 | + }) |
| 74 | + |
| 75 | + expect(code).toBe('wrapped') |
| 76 | + expect(jsHandler).toHaveBeenCalledTimes(1) |
| 77 | + expect(jsHandler.mock.calls[0]?.[2]).toEqual({ |
| 78 | + wrapExpression: true, |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + it('retries once with wrapExpression when the initial parse fails', () => { |
| 83 | + const jsHandler = vi |
| 84 | + .fn() |
| 85 | + .mockReturnValueOnce({ |
| 86 | + code: 'initial', |
| 87 | + error: new Error('parse error'), |
| 88 | + }) |
| 89 | + .mockReturnValueOnce({ |
| 90 | + code: 'fallback', |
| 91 | + }) |
| 92 | + const runtimeSet = new Set<string>() |
| 93 | + const code = generateCode(`{'foo': flag}`, { |
| 94 | + jsHandler, |
| 95 | + runtimeSet, |
| 96 | + }) |
| 97 | + |
| 98 | + expect(code).toBe('fallback') |
| 99 | + expect(jsHandler).toHaveBeenCalledTimes(2) |
| 100 | + expect(jsHandler.mock.calls[0]?.[2]).toBeUndefined() |
| 101 | + expect(jsHandler.mock.calls[1]?.[2]).toEqual({ |
| 102 | + wrapExpression: true, |
| 103 | + }) |
| 104 | + }) |
57 | 105 | }) |
0 commit comments