twd-js 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -72,10 +72,10 @@ pnpm add twd-js
72
72
  if (import.meta.env.DEV) {
73
73
  // Use Vite's glob import to find all test files
74
74
  const testModules = import.meta.glob("./**/*.twd.test.ts");
75
- const { initViteLoadTests, twd } = await import('twd-js');
76
- // Initialize the TWD sidebar and load tests
77
- initViteLoadTests(testModules, { open: true, position: 'left' });
78
- // Optionally initialize request mocking
75
+ const { initTests, twd, TWDSidebar } = await import('twd-js');
76
+ // You need to pass the test modules, the sidebar component, and createRoot function
77
+ initTests(testModules, <TWDSidebar open={true} position="left" />, createRoot);
78
+ // if you want to use mock requests, you can initialize it here
79
79
  twd.initRequestMocking()
80
80
  .then(() => {
81
81
  console.log("Request mocking initialized");
@@ -88,7 +88,6 @@ pnpm add twd-js
88
88
  createRoot(document.getElementById("root")!).render(
89
89
  <StrictMode>
90
90
  <App />
91
- <TWDSidebar open={false} />
92
91
  </StrictMode>
93
92
  );
94
93
  ```
@@ -140,9 +139,9 @@ pnpm add twd-js
140
139
  if (import.meta.env.DEV) {
141
140
  // Use Vite's glob import to find all test files
142
141
  const testModules = import.meta.glob("./**/*.twd.test.ts");
143
- const { initViteLoadTests, twd } = await import('twd-js');
144
- // Initialize the TWD sidebar and load tests
145
- initViteLoadTests(testModules, { open: true, position: 'left' });
142
+ const { initTests, twd, TWDSidebar } = await import('twd-js');
143
+ // You need to pass the test modules, the sidebar component, and createRoot function
144
+ initTests(testModules, <TWDSidebar open={true} position="left" />, createRoot);
146
145
  // Optionally initialize request mocking
147
146
  twd.initRequestMocking()
148
147
  .then(() => {
@@ -169,8 +168,8 @@ pnpm add twd-js
169
168
  './app.twd.test.ts': () => import('./app.twd.test'),
170
169
  './another-test-file.twd.test.ts': () => import('./another-test-file.twd.test'),
171
170
  };
172
- const { initViteLoadTests } = await import('twd-js');
173
- initViteLoadTests(testModules);
171
+ const { initTests, TWDSidebar } = await import('twd-js');
172
+ initTests(testModules, <TWDSidebar open={true} position="left" />, createRoot);
174
173
  }
175
174
  ```
176
175
 
@@ -6,7 +6,7 @@ export type Rule = {
6
6
  executed?: boolean;
7
7
  request?: unknown;
8
8
  status?: number;
9
- headers?: Record<string, string>;
9
+ responseHeaders?: Record<string, string>;
10
10
  urlRegex?: boolean;
11
11
  };
12
12
  export interface Options {
@@ -14,7 +14,7 @@ export interface Options {
14
14
  url: string | RegExp;
15
15
  response: unknown;
16
16
  status?: number;
17
- headers?: Record<string, string>;
17
+ responseHeaders?: Record<string, string>;
18
18
  urlRegex?: boolean;
19
19
  }
20
20
  /**
@@ -31,7 +31,8 @@ export declare const initRequestMocking: () => Promise<void>;
31
31
  * - `url`: URL string or RegExp to match
32
32
  * - `response`: Body of the mocked response
33
33
  * - `status`: (optional) HTTP status code (default: 200)
34
- * - `headers`: (optional) Response headers
34
+ * - `responseHeaders`: (optional) Response headers
35
+ * - `urlRegex`: (optional) Whether the URL is a regex (default: false)
35
36
  *
36
37
  * @example
37
38
  * ```ts
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './twd';
2
- export { initViteLoadTests } from './initializers/viteLoadTests';
2
+ export { TWDSidebar } from './ui/TWDSidebar';
3
+ export { initTests } from './initializers/initTests';
3
4
  export { expect } from 'chai';
4
5
  export { userEvent } from './proxies/userEvent';
@@ -1,6 +1,8 @@
1
1
  interface Options {
2
- open: boolean;
3
- position?: 'left' | 'right';
2
+ Component: React.ReactNode;
3
+ createRoot: (el: HTMLElement) => {
4
+ render: (el: React.ReactNode) => void;
5
+ };
4
6
  }
5
7
  /**
6
8
  * Initialize the TWD sidebar.
@@ -10,8 +12,11 @@ interface Options {
10
12
  * import { initSidebar } from 'twd-js';
11
13
  *
12
14
  * // Initialize the sidebar (e.g., in your main app file)
13
- * initSidebar({ open: false, position: 'left' });
15
+ * initSidebar({
16
+ * Component: <TWDSidebar open={true} position="left" />,
17
+ * createRoot,
18
+ * });
14
19
  * ```
15
20
  */
16
- export declare const initSidebar: (options: Options) => Promise<void>;
21
+ export declare const initSidebar: (options: Options) => void;
17
22
  export {};
@@ -1,7 +1,3 @@
1
- interface Options {
2
- open: boolean;
3
- position?: 'left' | 'right';
4
- }
5
1
  /**
6
2
  * A record of test module paths to their loader functions.
7
3
  * Each function returns a promise that resolves when the module is loaded.
@@ -18,15 +14,18 @@ type TestModule = Record<string, () => Promise<unknown>>;
18
14
  /**
19
15
  * Initialize Vite test loading.
20
16
  * @param testModules - The test modules to load.
21
- * @param options - Options for initializing the test loader.
17
+ * @param component - The React component to render the sidebar.
18
+ * @param createRoot - Function to create a React root.
22
19
  * @example
23
20
  * ```ts
24
21
  * if (import.meta.env.DEV) {
25
22
  * const testModules = import.meta.glob("./example.twd.test.ts");
26
- * const { initViteLoadTests } = await import('twd-js');
27
- * await initViteLoadTests(testModules, { open: false, position: 'left' });
23
+ * const { initTests, TWDSidebar } = await import('twd-js');
24
+ * await initTests(testModules, <TWDSidebar open={true} position="left" />, createRoot);
28
25
  * }
29
26
  * ```
30
27
  */
31
- export declare const initViteLoadTests: (testModules: TestModule, options: Options) => Promise<void>;
28
+ export declare const initTests: (testModules: TestModule, Component: React.ReactNode, createRoot: (el: HTMLElement) => {
29
+ render: (el: React.ReactNode) => void;
30
+ }) => Promise<void>;
32
31
  export {};
package/dist/mock-sw.js CHANGED
@@ -1 +1 @@
1
- function c(t,s,n){return n.find(e=>{const l=e.method.toLowerCase()===t.toLowerCase();if(e.urlRegex){const r=new RegExp(e.url);return l&&r.test(s)}const a=e.url===s||s.includes(e.url);return l&&a})}function i(t,s,n){t.forEach(e=>e.postMessage({type:"EXECUTED",alias:s.alias,request:n}))}let o=[];const u=async t=>{const{method:s}=t.request,n=t.request.url,e=c(s,n,o);e&&(console.log("Mock hit:",e.alias,s,n),t.respondWith((async()=>{let l=null;try{l=await t.request.clone().text()}catch{}return self.clients.matchAll().then(a=>{i(a,e,l)}),new Response(JSON.stringify(e.response),{status:e.status||200,headers:e.headers||{"Content-Type":"application/json"}})})()))},d=t=>{const{type:s,rule:n}=t.data||{};s==="ADD_RULE"&&(o=o.filter(e=>e.alias!==n.alias),o.push(n),console.log("Rule added:",n)),s==="CLEAR_RULES"&&(o=[],console.log("All rules cleared"))};self.addEventListener("fetch",u);self.addEventListener("message",d);
1
+ function u(e,s,o){return o.find(t=>{const a=t.method.toLowerCase()===e.toLowerCase();if(t.urlRegex){const n=new RegExp(t.url);return a&&n.test(s)}const l=t.url===s||s.includes(t.url);return a&&l})}function d(e,s,o){e.forEach(t=>t.postMessage({type:"EXECUTED",alias:s.alias,request:o}))}let c=[];const f=async e=>{const{method:s}=e.request,o=e.request.url,t=u(s,o,c);t&&(console.log("[TWD] Mock hit:",t.alias,s,o),e.respondWith((async()=>{let a=null;const l=e.request.headers.get("content-type")||"application/json";if(l.includes("application/json"))try{a=await e.request.clone().json()}catch{}else if(l.includes("form"))try{const n=await e.request.clone().formData();a={},n.forEach((r,i)=>{a[i]=r})}catch{}else if(l.includes("text"))try{a=await e.request.clone().text()}catch{}else if(l.includes("octet-stream"))try{a=await e.request.clone().arrayBuffer()}catch{}else if(l.includes("image"))try{a=await e.request.clone().blob()}catch{}else try{a=await e.request.clone().text()}catch{}return self.clients.matchAll().then(n=>{d(n,t,a)}),new Response(JSON.stringify(t.response),{status:t.status||200,headers:t.responseHeaders||{"Content-Type":"application/json"}})})()))},h=e=>{const{type:s,rule:o}=e.data||{};s==="ADD_RULE"&&(c=c.filter(t=>t.alias!==o.alias),c.push(o),console.log("[TWD] Rule added:",o)),s==="CLEAR_RULES"&&(c=[],console.log("[TWD] All rules cleared"))};console.log("[TWD] Mock Service Worker loaded - version 0.5.2");self.addEventListener("fetch",f);self.addEventListener("message",h);