11import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings" ;
22import { act , StrictMode , type ReactNode } from "react" ;
3- import { create , type ReactTestRenderer } from "react-test-renderer " ;
3+ import { createRoot , type Root } from "react-dom/client " ;
44import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
5+ import { page , userEvent } from "vite-plus/test/browser" ;
56
7+ import "../../index.css" ;
68import type { ScopedSettingsPatch } from "./scopedSettings" ;
79
810type WritingStyle = typeof DEFAULT_UNIFIED_SETTINGS . sourceControlWritingStyle ;
@@ -11,90 +13,77 @@ const state = vi.hoisted(() => ({
1113 updateSettings : vi . fn < ( patch : ScopedSettingsPatch ) => void > ( ) ,
1214} ) ) ;
1315
14- vi . mock ( "@tanstack/react-router" , ( ) => ( { useNavigate : ( ) => vi . fn ( ) } ) ) ;
15- vi . mock ( "./useScopedSettings" , ( ) => ( {
16- useScopedSettings : ( ) => ( {
16+ vi . mock ( import ( "@tanstack/react-router" ) , async ( original ) => ( {
17+ ...( await original ( ) ) ,
18+ useNavigate : ( ( ) => vi . fn ( ) ) as never ,
19+ } ) ) ;
20+ vi . mock ( import ( "./useScopedSettings" ) , async ( original ) => ( {
21+ ...( await original ( ) ) ,
22+ useScopedSettings : ( ( ) => ( {
1723 ...DEFAULT_UNIFIED_SETTINGS ,
1824 sourceControlWritingStyle : state . styles [ 0 ] ,
19- } ) ,
20- useScopedSettingsMixed : ( keys : ReadonlyArray < string > ) =>
25+ } ) ) as never ,
26+ useScopedSettingsMixed : ( keys ) =>
2127 ! keys . includes ( "reviewGuidesEnabled" ) &&
2228 JSON . stringify ( state . styles [ 0 ] ) !== JSON . stringify ( state . styles [ 1 ] ) ,
2329 useUpdateScopedSettings : ( ) => state . updateSettings ,
2430} ) ) ;
25- vi . mock ( "./SettingsScopeContext" , ( ) => ( {
26- useSettingsScope : ( ) => ( {
31+ vi . mock ( import ( "./SettingsScopeContext" ) , async ( original ) => ( {
32+ ...( await original ( ) ) ,
33+ useSettingsScope : ( ( ) => ( {
2734 scope : { kind : "all" , environmentIds : [ ] } ,
2835 environment : null ,
2936 connectedEnvironments : [ ] ,
3037 targets : state . styles . map ( ( style ) => ( {
3138 settings : { ...DEFAULT_UNIFIED_SETTINGS , sourceControlWritingStyle : style } ,
3239 } ) ) ,
33- } ) ,
40+ target : null ,
41+ } ) ) as never ,
3442} ) ) ;
35- vi . mock ( "./useScopedModelAvailability" , ( ) => ( {
36- useScopedModelDisabledReason : ( ) => ( ) => null ,
43+ vi . mock ( import ( "../../state/environments" ) , async ( original ) => ( {
44+ ...( await original ( ) ) ,
45+ useEnvironmentProviders : ( ) => new Map ( ) ,
3746} ) ) ;
38- vi . mock ( "../ui/toast" , ( ) => ( { toastManager : { add : vi . fn ( ) } } ) ) ;
39- vi . mock ( "../../state/server" , ( ) => ( { EMPTY_SERVER_PROVIDERS : [ ] } ) ) ;
40- vi . mock ( "../../state/environments" , ( ) => ( { useEnvironmentProviders : ( ) => new Map ( ) } ) ) ;
41- vi . mock ( "../chat/ProviderModelPicker" , ( ) => ( { ProviderModelPicker : ( ) => null } ) ) ;
42- vi . mock ( "./settingsSearch" , ( ) => ( { searchableSetting : ( id : string ) => ( { id, title : id } ) } ) ) ;
43- vi . mock ( "./settingsLayout" , ( ) => ( {
44- SETTINGS_PICKER_TRIGGER_CLASSNAME : "" ,
45- SettingResetButton : ( { label, onClick } : { label : string ; onClick : ( ) => void } ) => (
46- < button onClick = { onClick } > { `Reset ${ label } ` } </ button >
47- ) ,
48- SettingsSection : ( { children } : { children : ReactNode } ) => children ,
49- SettingsRow : ( {
50- children,
51- control,
52- resetAction,
53- } : {
54- children : ReactNode ;
55- control : ReactNode ;
56- resetAction : ReactNode ;
57- } ) => (
47+ // The real row reads write permission and overrides from live environment state; this
48+ // suite is about how the section writes mixed writing styles.
49+ vi . mock ( import ( "./settingsLayout" ) , async ( original ) => ( {
50+ ...( await original ( ) ) ,
51+ SettingsRow : ( { title, control, resetAction, children } ) => (
5852 < div >
53+ < h3 > { title } </ h3 >
5954 { control }
6055 { resetAction }
6156 { children }
6257 </ div >
6358 ) ,
59+ SettingsSection : ( ( { children } : { children : ReactNode } ) => children ) as never ,
6460} ) ) ;
65- vi . mock ( "../ui/select" , ( ) => ( {
66- Select : ( { children } : { children : ReactNode } ) => children ,
67- SelectItem : "span" ,
68- SelectPopup : "div" ,
69- SelectTrigger : "div" ,
70- SelectValue : "span" ,
71- } ) ) ;
72- vi . mock ( "../ui/switch" , ( ) => ( { Switch : "input" } ) ) ;
73- vi . mock ( "../ui/textarea" , ( ) => ( { Textarea : "textarea" } ) ) ;
74- vi . mock ( "../ui/button" , ( ) => ( { Button : "button" } ) ) ;
7561
7662import { SourceControlWritingSettingsSection } from "./SourceControlWritingSettings" ;
7763
78- let renderer : ReactTestRenderer | null ;
79-
80- function button ( label : string ) {
81- return renderer ! . root . findAllByType ( "button" ) . find ( ( item ) => item . children . includes ( label ) ) ! ;
82- }
64+ let root : Root ;
65+ let container : HTMLDivElement ;
8366
84- function openEditor ( ) {
85- act ( ( ) => button ( "Write custom instructions for all" ) . props . onClick ( ) ) ;
86- }
87-
88- function editInstructions ( value : string ) {
89- act ( ( ) => renderer ! . root . findByType ( "textarea" ) . props . onChange ( { target : { value } } ) ) ;
90- }
67+ const resetButton = ( label : string ) =>
68+ page . getByRole ( "button" , { name : `Reset ${ label } to default` } ) ;
69+ const writeForAll = ( ) => page . getByRole ( "button" , { name : "Write custom instructions for all" } ) ;
70+ const applyToAll = ( ) => page . getByRole ( "button" , { name : "Apply instructions to all" } ) ;
71+ const bulkEditor = ( ) =>
72+ page . getByRole ( "textbox" , {
73+ name : "Custom source control instructions for all selected environments" ,
74+ } ) ;
9175
92- function applyInstructions ( ) {
93- act ( ( ) => button ( "Apply instructions to all" ) . props . onClick ( ) ) ;
76+ async function render ( ) {
77+ await act ( async ( ) =>
78+ root . render (
79+ < StrictMode >
80+ < SourceControlWritingSettingsSection />
81+ </ StrictMode > ,
82+ ) ,
83+ ) ;
9484}
9585
96- beforeEach ( ( ) => {
97- vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
86+ beforeEach ( async ( ) => {
9887 state . styles = [
9988 {
10089 ...DEFAULT_UNIFIED_SETTINGS . sourceControlWritingStyle ,
@@ -111,30 +100,27 @@ beforeEach(() => {
111100 ] ;
112101 state . updateSettings . mockReset ( ) . mockImplementation ( ( patch ) => {
113102 state . styles = state . styles . map ( ( style ) => ( { ...style , ...patch . sourceControlWritingStyle } ) ) ;
103+ void render ( ) ;
114104 } ) ;
115- act ( ( ) => {
116- renderer = create (
117- < StrictMode >
118- < SourceControlWritingSettingsSection />
119- </ StrictMode > ,
120- ) ;
121- } ) ;
105+ container = document . createElement ( "div" ) ;
106+ document . body . append ( container ) ;
107+ root = createRoot ( container ) ;
108+ await render ( ) ;
122109} ) ;
123110
124111afterEach ( async ( ) => {
125- await act ( async ( ) => renderer ?. unmount ( ) ) ;
126- renderer = null ;
127- vi . unstubAllGlobals ( ) ;
112+ await act ( async ( ) => root . unmount ( ) ) ;
113+ container . remove ( ) ;
128114} ) ;
129115
130116describe ( "mixed source control instructions" , ( ) => {
131- it ( "resets mixed template preferences without replacing each environment's instructions" , ( ) => {
117+ it ( "resets mixed template preferences without replacing each environment's instructions" , async ( ) => {
132118 const initialInstructions = state . styles . map ( ( { mode, customInstructions } ) => ( {
133119 mode,
134120 customInstructions,
135121 } ) ) ;
136122
137- act ( ( ) => button ( "Reset change request templates") . props . onClick ( ) ) ;
123+ await userEvent . click ( resetButton ( " change request templates") ) ;
138124
139125 expect ( state . updateSettings ) . toHaveBeenCalledTimes ( 1 ) ;
140126 expect ( state . styles . map ( ( style ) => style . followChangeRequestTemplates ) ) . toEqual ( [
@@ -146,17 +132,11 @@ describe("mixed source control instructions", () => {
146132 ) . toEqual ( initialInstructions ) ;
147133 } ) ;
148134
149- it ( "resets every environment even when the representative already has default instructions" , ( ) => {
135+ it ( "resets every environment even when the representative already has default instructions" , async ( ) => {
150136 state . styles [ 0 ] = { ...DEFAULT_UNIFIED_SETTINGS . sourceControlWritingStyle } ;
151- act ( ( ) => {
152- renderer ! . update (
153- < StrictMode >
154- < SourceControlWritingSettingsSection />
155- </ StrictMode > ,
156- ) ;
157- } ) ;
137+ await render ( ) ;
158138
159- act ( ( ) => button ( "Reset source control writing style") . props . onClick ( ) ) ;
139+ await userEvent . click ( resetButton ( " source control writing style") ) ;
160140
161141 expect ( state . updateSettings ) . toHaveBeenCalledTimes ( 1 ) ;
162142 expect (
@@ -170,22 +150,21 @@ describe("mixed source control instructions", () => {
170150 expect ( state . styles [ 1 ] ! . followChangeRequestTemplates ) . toBe ( false ) ;
171151 } ) ;
172152
173- it ( "does not write an untouched bulk draft" , ( ) => {
153+ it ( "does not write an untouched bulk draft" , async ( ) => {
174154 const initialStyles = state . styles ;
175- openEditor ( ) ;
176- expect ( renderer ! . root . findByType ( "textarea" ) . props . value ) . toBe ( "" ) ;
177- expect ( button ( "Apply instructions to all" ) . props . disabled ) . toBe ( true ) ;
155+ await userEvent . click ( writeForAll ( ) ) ;
156+ await expect . element ( bulkEditor ( ) ) . toHaveValue ( "" ) ;
157+ await expect . element ( applyToAll ( ) ) . toBeDisabled ( ) ;
178158
179- applyInstructions ( ) ;
180159 expect ( state . updateSettings ) . not . toHaveBeenCalled ( ) ;
181160 expect ( state . styles ) . toEqual ( initialStyles ) ;
182161 } ) ;
183162
184- it ( "applies edited instructions to every selected environment" , ( ) => {
185- openEditor ( ) ;
186- editInstructions ( " Keep titles concise. " ) ;
187- expect ( button ( "Apply instructions to all" ) . props . disabled ) . toBe ( false ) ;
188- applyInstructions ( ) ;
163+ it ( "applies edited instructions to every selected environment" , async ( ) => {
164+ await userEvent . click ( writeForAll ( ) ) ;
165+ await userEvent . fill ( bulkEditor ( ) , " Keep titles concise. " ) ;
166+ await expect . element ( applyToAll ( ) ) . toBeEnabled ( ) ;
167+ await userEvent . click ( applyToAll ( ) ) ;
189168
190169 expect ( state . updateSettings ) . toHaveBeenCalledTimes ( 1 ) ;
191170 expect ( state . styles . map ( ( style ) => style . customInstructions ) ) . toEqual ( [
@@ -195,28 +174,30 @@ describe("mixed source control instructions", () => {
195174 expect ( state . styles . map ( ( style ) => style . followChangeRequestTemplates ) ) . toEqual ( [ true , false ] ) ;
196175 } ) ;
197176
198- it ( "allows an intentional clear after editing" , ( ) => {
199- openEditor ( ) ;
200- editInstructions ( "Temporary instructions" ) ;
201- editInstructions ( "" ) ;
202- expect ( button ( "Apply instructions to all" ) . props . disabled ) . toBe ( false ) ;
203- applyInstructions ( ) ;
177+ it ( "allows an intentional clear after editing" , async ( ) => {
178+ await userEvent . click ( writeForAll ( ) ) ;
179+ await userEvent . fill ( bulkEditor ( ) , "Temporary instructions" ) ;
180+ await userEvent . clear ( bulkEditor ( ) ) ;
181+ await expect . element ( applyToAll ( ) ) . toBeEnabled ( ) ;
182+ await userEvent . click ( applyToAll ( ) ) ;
204183
205184 expect ( state . updateSettings ) . toHaveBeenCalledTimes ( 1 ) ;
206185 expect ( state . styles . map ( ( style ) => style . customInstructions ) ) . toEqual ( [ "" , "" ] ) ;
207186 } ) ;
208187
209- it ( "shows the plain editor once instructions agree, even while templates differ" , ( ) => {
210- openEditor ( ) ;
211- editInstructions ( "Shared instructions" ) ;
212- applyInstructions ( ) ;
188+ it ( "shows the plain editor once instructions agree, even while templates differ" , async ( ) => {
189+ await userEvent . click ( writeForAll ( ) ) ;
190+ await userEvent . fill ( bulkEditor ( ) , "Shared instructions" ) ;
191+ await userEvent . click ( applyToAll ( ) ) ;
213192
214193 expect ( state . updateSettings ) . toHaveBeenCalledTimes ( 1 ) ;
215194 expect ( state . styles . map ( ( style ) => style . customInstructions ) ) . toEqual ( [
216195 "Shared instructions" ,
217196 "Shared instructions" ,
218197 ] ) ;
219- expect ( button ( "Write custom instructions for all" ) ) . toBeUndefined ( ) ;
220- expect ( renderer ! . root . findByType ( "textarea" ) . props . defaultValue ) . toBe ( "Shared instructions" ) ;
198+ await expect . element ( writeForAll ( ) ) . not . toBeInTheDocument ( ) ;
199+ await expect
200+ . element ( page . getByRole ( "textbox" , { name : "Custom source control writing instructions" } ) )
201+ . toHaveValue ( "Shared instructions" ) ;
221202 } ) ;
222203} ) ;
0 commit comments