|
| 1 | +import { expect, it } from 'vitest' |
| 2 | +import { allChanges, compareFiles, config } from '#design-diff/tests/helpers' |
| 3 | + |
| 4 | +const view = 'apps/sim/page.tsx' |
| 5 | +const settings = { ...config, themes: [] } |
| 6 | + |
| 7 | +it.each([ |
| 8 | + ['named', "import {useState} from 'react'", 'useState'], |
| 9 | + ['alias', "import {useState as state} from 'react'", 'state'], |
| 10 | + ['namespace', "import * as React from 'react'", 'React.useState'], |
| 11 | + ['default', "import React from 'react'", 'React.useState'], |
| 12 | +])('traces error copy assigned through a %s React state binding', async (_name, imports, hook) => { |
| 13 | + const source = (message: string) => `${imports};const ERROR='${message}'; |
| 14 | + export function Page(){const [errors,setErrors]=${hook}([]); |
| 15 | + async function submit(){try {await signIn()}catch {setErrors([ERROR])}} |
| 16 | + return <form onSubmit={submit}><span>{errors.join(', ')}</span></form>}` |
| 17 | + const report = await compareFiles( |
| 18 | + { [view]: source('Try again') }, |
| 19 | + { [view]: source('Check your email and try again') }, |
| 20 | + settings |
| 21 | + ) |
| 22 | + expect(report.flagged).toBe(true) |
| 23 | + expect(allChanges(report).some((change) => change.after?.location.file === view)).toBe(true) |
| 24 | +}) |
| 25 | + |
| 26 | +it.each([ |
| 27 | + ['colour', "'red'", "'blue'", 'style={{color:value}}'], |
| 28 | + ['padding', '2', '4', 'style={{padding:value}}'], |
| 29 | + ['visibility', 'false', 'true', 'hidden={value}'], |
| 30 | + ['functional update', "old => old + 'a'", "old => old + 'b'", 'title={value}'], |
| 31 | +])('traces state-driven %s changes through setter aliases', async (_name, before, after, prop) => { |
| 32 | + const source = (update: string) => `import {useState} from 'react'; |
| 33 | + export function Page(){const [value,setValue]=useState(null);const update=setValue; |
| 34 | + function click(){update(${update})}return <button onClick={click} ${prop}/>}` |
| 35 | + expect( |
| 36 | + (await compareFiles({ [view]: source(before) }, { [view]: source(after) }, settings)).flagged |
| 37 | + ).toBe(true) |
| 38 | +}) |
| 39 | + |
| 40 | +it('keeps unrelated handler telemetry and unrendered state clean', async () => { |
| 41 | + const source = (message: string) => `import {useState} from 'react'; |
| 42 | + export function Page(){const [value,setValue]=useState('ready'); |
| 43 | + const [unused,setUnused]=useState('');function click(){logger.info('${message}'); |
| 44 | + setUnused('${message}');setValue('done')}return <button onClick={click}>{value}</button>}` |
| 45 | + expect( |
| 46 | + (await compareFiles({ [view]: source('before') }, { [view]: source('after') }, settings)) |
| 47 | + .flagged |
| 48 | + ).toBe(false) |
| 49 | +}) |
| 50 | + |
| 51 | +it('retains conditions controlling a rendered state update', async () => { |
| 52 | + const source = (guard: string) => `import {useState} from 'react'; |
| 53 | + export function Page({a,b}){const [value,setValue]=useState('ready'); |
| 54 | + function click(){if(${guard})setValue('done')}return <button onClick={click}>{value}</button>}` |
| 55 | + expect( |
| 56 | + (await compareFiles({ [view]: source('a') }, { [view]: source('b') }, settings)).flagged |
| 57 | + ).toBe(true) |
| 58 | +}) |
| 59 | + |
| 60 | +it('does not trust a local function merely named useState', async () => { |
| 61 | + const source = (message: string) => `function useState(){return ['ready',logger.info]} |
| 62 | + export function Page(){const [value,setValue]=useState(); |
| 63 | + function click(){setValue('${message}')}return <button onClick={click}>{value}</button>}` |
| 64 | + expect( |
| 65 | + (await compareFiles({ [view]: source('before') }, { [view]: source('after') }, settings)) |
| 66 | + .flagged |
| 67 | + ).toBe(false) |
| 68 | +}) |
| 69 | + |
| 70 | +it('retains the branch containing a state update', async () => { |
| 71 | + const source = (body: string) => `import {useState} from 'react'; |
| 72 | + export function Page({ready}){const [value,setValue]=useState('ready'); |
| 73 | + function click(){${body}}return <button onClick={click}>{value}</button>}` |
| 74 | + expect( |
| 75 | + ( |
| 76 | + await compareFiles( |
| 77 | + { [view]: source("if(ready){setValue('done')}else{}") }, |
| 78 | + { [view]: source("if(ready){}else{setValue('done')}") }, |
| 79 | + settings |
| 80 | + ) |
| 81 | + ).flagged |
| 82 | + ).toBe(true) |
| 83 | +}) |
| 84 | + |
| 85 | +it('keeps a state/setter rename and constant hoist equivalent', async () => { |
| 86 | + const before = `import {useState} from 'react';export function Page(){ |
| 87 | + const [value,setValue]=useState('ready');function click(){setValue('done')} |
| 88 | + return <button onClick={click}>{value}</button>}` |
| 89 | + const after = `import {useState} from 'react';const DONE='done';export function Page(){ |
| 90 | + const [label,setLabel]=useState('ready');function click(){setLabel(DONE)} |
| 91 | + return <button onClick={click}>{label}</button>}` |
| 92 | + expect((await compareFiles({ [view]: before }, { [view]: after }, settings)).flagged).toBe(false) |
| 93 | +}) |
0 commit comments