test: добавить E2E-тест для множественных окружений и исправить синхронизацию AppState
This commit is contained in:
42
src/App.tsx
42
src/App.tsx
@@ -18,17 +18,18 @@ class AppState {
|
||||
static readonly Instance = new AppState();
|
||||
|
||||
public loadConfig(cfg: Config) {
|
||||
this.envs = [...cfg.envs];
|
||||
this.config = cfg;
|
||||
this.envs = cfg.envs; // Reference the same array as config
|
||||
}
|
||||
|
||||
public addEnv(env: Env): number {
|
||||
this.envs.push(env);
|
||||
// Also update config.envs since they reference the same array
|
||||
this.config.envs = this.envs;
|
||||
return this.envs.length - 1;
|
||||
}
|
||||
|
||||
public async saveEnv(env: Env): Promise<number> {
|
||||
|
||||
// Create a promise that resolves after 1 second
|
||||
return await new Promise<number>((resolve) => {
|
||||
setTimeout(() => {
|
||||
@@ -41,6 +42,21 @@ class AppState {
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
|
||||
public removeEnv(envId: number) {
|
||||
const idx = this.envs.findIndex(x => x.id === envId);
|
||||
if (idx > -1) {
|
||||
this.envs.splice(idx, 1);
|
||||
// Also update config.envs since they reference the same array
|
||||
this.config.envs = this.envs;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the internal envs reference to match React state
|
||||
public syncEnvs(newEnvs: Env[]) {
|
||||
this.envs = newEnvs;
|
||||
this.config.envs = newEnvs;
|
||||
}
|
||||
}
|
||||
|
||||
function App() {
|
||||
@@ -57,7 +73,12 @@ function App() {
|
||||
let idx = AppState.Instance.envs.findIndex(x => x.id === env.id);
|
||||
if (idx > -1) {
|
||||
AppState.Instance.envs[idx] = env;
|
||||
setEnvs([...AppState.Instance.envs]);
|
||||
// Also update config.envs since they reference the same array
|
||||
AppState.Instance.config.envs = AppState.Instance.envs;
|
||||
const newEnvs = [...AppState.Instance.envs];
|
||||
setEnvs(newEnvs);
|
||||
AppState.Instance.syncEnvs(newEnvs); // Keep AppState in sync with React state
|
||||
setConfig(AppState.Instance.config); // Trigger re-render for config
|
||||
}
|
||||
|
||||
// Then do the async save (for consistency with existing behavior)
|
||||
@@ -70,16 +91,19 @@ function App() {
|
||||
|
||||
function handleEnvAdded(env: Env): number {
|
||||
const idx = AppState.Instance.addEnv(env);
|
||||
setEnvs([...AppState.Instance.envs]);
|
||||
const newEnvs = [...AppState.Instance.envs];
|
||||
setEnvs(newEnvs);
|
||||
AppState.Instance.syncEnvs(newEnvs); // Keep AppState in sync with React state
|
||||
setConfig(AppState.Instance.config); // Trigger re-render for config
|
||||
return idx;
|
||||
}
|
||||
|
||||
function handleEnvRemoved(envId: number) {
|
||||
const idx = AppState.Instance.envs.findIndex(x => x.id === envId);
|
||||
if (idx > -1) {
|
||||
AppState.Instance.envs.splice(idx, 1);
|
||||
setEnvs([...AppState.Instance.envs]);
|
||||
}
|
||||
AppState.Instance.removeEnv(envId);
|
||||
const newEnvs = [...AppState.Instance.envs];
|
||||
setEnvs(newEnvs);
|
||||
AppState.Instance.syncEnvs(newEnvs); // Keep AppState in sync with React state
|
||||
setConfig(AppState.Instance.config); // Trigger re-render for config
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user