1
0
mirror of https://github.com/bitwarden/browser synced 2026-01-27 06:43:41 +00:00

Improve scene type visibility

This commit is contained in:
Matt Gibson
2026-01-26 16:55:51 -08:00
parent 01bd551888
commit ab8d2b05d1
4 changed files with 22 additions and 16 deletions

View File

@@ -24,8 +24,10 @@ export class Play {
* @param options Options for the scene
* @returns
*/
static async scene<TUp, TResult>(template: SceneTemplate<TUp, TResult>): Promise<Scene<TResult>> {
const scene = new Scene<TResult>();
static async scene<TUp, TResult>(
template: SceneTemplate<TUp, TResult>,
): Promise<Scene<TUp, TResult>> {
const scene = new Scene<TUp, TResult>();
await scene.init(template);
return scene;
}

View File

@@ -3,6 +3,8 @@ import { webServerBaseUrl } from "@playwright-config";
// First seed points at the seeder API proxy, second is the seed path of the SeedController
const seedApiUrl = new URL("/seed/seed/", webServerBaseUrl).toString();
export type extractTUpType<T> = T extends SceneTemplate<infer U, any> ? U : never;
export abstract class SceneTemplate<TUp, TReturns = void> {
abstract template: string;
private seedId?: string;
@@ -14,7 +16,7 @@ export abstract class SceneTemplate<TUp, TReturns = void> {
return this.seedId;
}
constructor(private upArgs: TUp) {}
constructor(public upArgs: TUp) {}
async up(): Promise<SceneTemplateResult<TReturns>> {
const result = await sceneUp<TUp, TReturns>(this.template, this.upArgs);
this.seedId = result.seedId;

View File

@@ -5,16 +5,14 @@ import { Scene } from "../scene";
import { SceneTemplate } from "./scene-template";
type SceneResult = UserId;
type UpParams = {
email: string;
emailVerified?: boolean;
premium?: boolean;
};
export type SingleUserScene = Scene<SceneResult>;
export type SingleUserScene = Scene<UpParams, SceneResult>;
export class SingleUserSceneTemplate extends SceneTemplate<
{
email: string;
emailVerified?: boolean;
premium?: boolean;
},
SceneResult
> {
export class SingleUserSceneTemplate extends SceneTemplate<UpParams, SceneResult> {
template: string = "SingleUserScene";
}

View File

@@ -10,15 +10,15 @@ import { SceneTemplate } from "./scene-templates/scene-template";
* - {@link SceneOptions.noDown}: Useful for setting up data then using codegen to create tests that use the data. Remember to tear down the data manually.
* - {@link SceneOptions.downAfterAll}: Useful for expensive setups that you want to share across all tests in a worker or for writing acts.
*/
export class Scene<Returns = void> {
export class Scene<UpParams = unknown, Returns = void> {
private inited = false;
private _template?: SceneTemplate<unknown, Returns>;
private _template?: SceneTemplate<UpParams, Returns>;
private mangledMap = new Map<string, string | null>();
private _returnValue?: Returns;
constructor() {}
private get template(): SceneTemplate<unknown, Returns> {
private get template(): SceneTemplate<UpParams, Returns> {
if (!this.inited) {
throw new Error("Scene must be initialized before accessing template");
}
@@ -28,6 +28,10 @@ export class Scene<Returns = void> {
return this._template;
}
get upArgs(): UpParams {
return this.template.upArgs;
}
get returnValue(): Returns {
if (!this.inited) {
throw new Error("Scene must be initialized before accessing returnValue");
@@ -43,7 +47,7 @@ export class Scene<Returns = void> {
return this.mangledMap.get(id) ?? id;
}
async init<T extends SceneTemplate<TUp, Returns>, TUp>(template: T): Promise<void> {
async init(template: SceneTemplate<UpParams, Returns>): Promise<void> {
if (this.inited) {
throw new Error("Scene has already been initialized");
}