twd-js 0.2.0 → 0.3.0

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
@@ -87,8 +87,17 @@ pnpm add twd-js
87
87
  - With Vite:
88
88
 
89
89
  ```ts
90
+ import { twd } from "twd-js";
90
91
  // src/loadTests.ts
91
- const modules = import.meta.glob("./**/*.twd.test.ts", { eager: true });
92
+ import.meta.glob("./**/*.twd.test.ts", { eager: true });
93
+ // Initialize request mocking once
94
+ twd.initRequestMocking()
95
+ .then(() => {
96
+ console.log("Request mocking initialized");
97
+ })
98
+ .catch((err) => {
99
+ console.error("Error initializing request mocking:", err);
100
+ });
92
101
  // No need to export anything
93
102
  ```
94
103
 
@@ -134,8 +143,11 @@ This will copy `mock-sw.js` to your public directory.
134
143
  Just call `await twd.initRequestMocking()` at the start of your test, then use `twd.mockRequest` to define your mocks. Example:
135
144
 
136
145
  ```ts
146
+ import { describe, it, twd, userEvent } from "twd-js";
147
+
137
148
  it("fetches a message", async () => {
138
- await twd.initRequestMocking();
149
+ twd.visit("/");
150
+ const user = userEvent.setup();
139
151
  await twd.mockRequest("message", {
140
152
  method: "GET",
141
153
  url: "https://api.example.com/message",
@@ -144,7 +156,7 @@ it("fetches a message", async () => {
144
156
  },
145
157
  });
146
158
  const btn = await twd.get("button[data-twd='message-button']");
147
- btn.click();
159
+ await user.click(btn.el);
148
160
  await twd.waitForRequest("message");
149
161
  const messageText = await twd.get("p[data-twd='message-text']");
150
162
  messageText.should("have.text", "Mocked message!");
package/dist/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './twd';
2
2
  export { TWDSidebar } from './ui/TWDSidebar';
3
+ export { expect } from 'chai';
4
+ export { userEvent } from './proxies/userEvent';
package/dist/mock-sw.js CHANGED
@@ -1 +1 @@
1
- function i(t,n,s){return s.find(e=>e.method===t&&(typeof e.url=="string"?e.url===n:new RegExp(e.url).test(n)))}function r(t,n,s){t.forEach(e=>e.postMessage({type:"EXECUTED",alias:n.alias,request:s}))}let l=[];self.addEventListener("fetch",t=>{const{method:n}=t.request,s=t.request.url,e=i(n,s,l);e&&(console.log("Mock hit:",e.alias,n,s),t.respondWith((async()=>{let a=null;try{a=await t.request.clone().text()}catch{}return self.clients.matchAll().then(o=>{r(o,e,a)}),new Response(JSON.stringify(e.response),{status:e.status||200,headers:e.headers||{"Content-Type":"application/json"}})})()))});self.addEventListener("message",t=>{const{type:n,rule:s}=t.data||{};n==="ADD_RULE"&&(l=l.filter(e=>e.alias!==s.alias),l.push(s),console.log("Rule added:",s))});
1
+ function i(s,t,l){return l.find(e=>{const o=e.method.toLowerCase()===s.toLowerCase(),a=typeof t=="string"?e.url===t||e.url.includes(t):new RegExp(t).test(e.url);return o&&a})}function r(s,t,l){s.forEach(e=>e.postMessage({type:"EXECUTED",alias:t.alias,request:l}))}let n=[];self.addEventListener("fetch",s=>{const{method:t}=s.request,l=s.request.url,e=i(t,l,n);e&&(console.log("Mock hit:",e.alias,t,l),s.respondWith((async()=>{let o=null;try{o=await s.request.clone().text()}catch{}return self.clients.matchAll().then(a=>{r(a,e,o)}),new Response(JSON.stringify(e.response),{status:e.status||200,headers:e.headers||{"Content-Type":"application/json"}})})()))});self.addEventListener("message",s=>{const{type:t,rule:l}=s.data||{};t==="ADD_RULE"&&(n=n.filter(e=>e.alias!==l.alias),n.push(l),console.log("Rule added:",l)),t==="CLEAR_RULES"&&(n=[],console.log("All rules cleared"))});
@@ -0,0 +1,4 @@
1
+ import { default as userEventLib } from '@testing-library/user-event';
2
+ type UserEvent = typeof userEventLib;
3
+ export declare const userEvent: UserEvent;
4
+ export {};
@@ -88,47 +88,6 @@ export type ShouldFn = {
88
88
  export interface TWDElemAPI {
89
89
  /** The underlying DOM element. */
90
90
  el: Element;
91
- /**
92
- * Simulates a user click on the element.
93
- * Returns the same API so you can chain more actions.
94
- *
95
- * @example
96
- * ```ts
97
- * const button = await twd.get("button");
98
- * button.click();
99
- *
100
- * ```
101
- *
102
- */
103
- click: () => void;
104
- /**
105
- * Types text into an input element.
106
- * @param text The text to type.
107
- * @returns The input element.
108
- *
109
- * @example
110
- * ```ts
111
- * const email = await twd.get("input#email");
112
- * email.type("test@example.com");
113
- *
114
- * ```
115
- *
116
- */
117
- type: (text: string) => HTMLInputElement | HTMLTextAreaElement;
118
- /**
119
- * Gets the text content of the element.
120
- * @returns The text content.
121
- *
122
- * @example
123
- * ```ts
124
- * const para = await twd.get("p");
125
- * const content = para.text();
126
- * console.log(content);
127
- *
128
- * ```
129
- *
130
- */
131
- text: () => string;
132
91
  /**
133
92
  * Asserts something about the element.
134
93
  * @param name The name of the assertion.
package/dist/twd.d.ts CHANGED
@@ -138,6 +138,16 @@ interface TWDAPI {
138
138
  * ```
139
139
  */
140
140
  getRequestMockRules: () => Rule[];
141
+ /**
142
+ * Waits for a specified time.
143
+ * @param time Time in milliseconds to wait
144
+ * @returns A promise that resolves after the specified time
145
+ * @example
146
+ * ```ts
147
+ * await twd.wait(500); // wait for 500ms
148
+ * ```
149
+ */
150
+ wait: (time: number) => Promise<void>;
141
151
  }
142
152
  /**
143
153
  * Mini Cypress-style helpers for DOM testing.