Skip to content

Commit

Permalink
Merge pull request #36 from andrewiggins/mock-fetch-ui
Browse files Browse the repository at this point in the history
Add UI for controlling mock fetch requests
  • Loading branch information
andrewiggins committed Aug 20, 2023
2 parents d24a315 + 6493f3a commit 5cf1bf3
Show file tree
Hide file tree
Showing 14 changed files with 1,219 additions and 98 deletions.
8 changes: 8 additions & 0 deletions jest-puppeteer.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const isDebug = process.env.PPTR_DEBUG === "true";

module.exports = {
launch: {
headless: !isDebug,
devtools: isDebug
}
};
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dev": "node ./scripts/dev.js",
"serve": "sirv --dev dist",
"test": "jest",
"test:debug": "cross-env PPTR_DEBUG=true jest --runInBand",
"prepare": "husky install",
"lint-staged": "lint-staged",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,html,vue}\""
Expand Down Expand Up @@ -39,6 +40,7 @@
"@rollup/plugin-terser": "^0.4.3",
"@types/jest": "^29.0.0",
"babel-jest": "^29.6.2",
"cross-env": "^7.0.3",
"html-minifier": "^4.0.0",
"husky": "^8.0.3",
"jest": "^29.6.2",
Expand Down
27 changes: 27 additions & 0 deletions scripts/bundles/controls/jsx.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type Children = undefined | string | JSX.Element | Array<Children>;

interface JSXAttributes {
children?: Children;

// Attributes that this JSX allows
class?: string;
for?: string;
id?: string;
}

type JSXHTMLElement<T> = Partial<Omit<HTMLElementTagNameMap[T], "children">>;

type HTMLElementsMap = {
[K in keyof HTMLElementTagNameMap]: JSXHTMLElement<K> & JSXAttributes;
};

declare namespace JSX {
interface Element extends HTMLElement {}
interface ElementChildrenAttribute {
children?: Children;
}
interface IntrinsicElements extends HTMLElementsMap {
// Custom elements
"draggable-dialog": JSXAttributes;
}
}
51 changes: 51 additions & 0 deletions scripts/bundles/controls/mockFetch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
interface Timer {
expiresAt: number;
timeoutId: number;
}

type RequestId = string;

interface Request {
id: RequestId;
/** When this request should resolve. If null, request is paused and not scheduled to complete */
expiresAt: number | null;
/** Total time this request should wait */
duration: number;
/** Tracks how much time of duration has elapsed when a request is paused/resumed */
elapsedTime: number;
/** Display name of request */
name: string;
url: string;
options: RequestInit;
promise: Promise<void>;
resolve: () => void;
reject: () => void;
}

type MockFetchEventType = "update";

interface Config {
durationMs: number;
areNewRequestsPaused: boolean;
mode: "auto" | "manual";
timer: Timer | null;
requests: Map<string, Request>;
newId(): string;
pause(id: RequestId): void;
resume(id: RequestId): void;
on(type: MockFetchEventType, handler: () => void): void;
off(type: MockFetchEventType, handler: () => void): void;
log(...msg: any[]): void;
_emit(type: MockFetchEventType): void;
}

declare class MockFetchDebugger extends HTMLElement {
config: Config;
show: boolean;
dialog: boolean;
}

export function createMockFetchConfig(): Config;
export function createMockFetch(
config: Config
): (url: string, options?: RequestInit) => Promise<void>;

0 comments on commit 5cf1bf3

Please sign in to comment.