forked from QwikDev/qwik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unit testing support (phase 1) (QwikDev#1874)
Co-authored-by: Leifer <leifer.jesus.mendez.zambrano@external.gloval.es>
- Loading branch information
1 parent
ef638ba
commit 7c9c65c
Showing
9 changed files
with
135 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
### @builder.io/qwik/testing | ||
|
||
```ts | ||
//vite.config.ts | ||
import { defineConfig } from 'vite'; | ||
import { qwikVite } from '@builder.io/qwik/optimizer'; | ||
import { qwikCity } from '@builder.io/qwik-city/vite'; | ||
import tsconfigPaths from 'vite-tsconfig-paths'; | ||
|
||
export default defineConfig(() => { | ||
return { | ||
plugins: [qwikCity(), qwikVite(), tsconfigPaths()], | ||
define: { | ||
'globalThis.qTest': true, | ||
'globalThis.qDev': true, | ||
}, | ||
}; | ||
}); | ||
``` | ||
|
||
```jsx | ||
// card.test.tsx | ||
|
||
import { createDOM } from '@builder.io/qwik/testing'; | ||
import { test, expect } from 'vitest'; | ||
import Card from './card.tsx'; | ||
|
||
test(`[Card Component]: 🙌 Only render`, async () => { | ||
const { screen, render } = await await createDOM(); | ||
await render(<Card />); | ||
expect(screen.outerHTML).toContain('Counter_0'); | ||
}); | ||
|
||
test(`[Card Component]: 🙌 Click counter +1 `, async () => { | ||
const { screen, render, userEvent } = await await createDOM(); | ||
await render(<Card />); | ||
expect(screen.outerHTML).toContain('Counter_0'); | ||
await userEvent('button.btn-counter', 'click'); | ||
expect(screen.outerHTML).toContain('Counter_1'); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1 @@ | ||
export { createDocument, createWindow } from './document'; | ||
export { ElementFixture } from './element-fixture'; | ||
export type { ElementFixtureOptions } from './element-fixture'; | ||
export { getTestPlatform } from './platform'; | ||
export type { | ||
MockDocument, | ||
MockDocumentOptions, | ||
MockWindow, | ||
MockWindowOptions, | ||
TestPlatform, | ||
} from './types'; | ||
export { toFileUrl } from './util'; | ||
export { createDOM } from './library'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { fromCamelToKebabCase } from './../core/util/case'; | ||
import { ElementFixture, dispatch } from './element-fixture'; | ||
import { setTestPlatform, getTestPlatform } from './platform'; | ||
import type { JSXNode } from '@builder.io/qwik/jsx-runtime'; | ||
|
||
/** | ||
* | ||
* @param root | ||
* @param selector | ||
* @param eventNameCamel | ||
*/ | ||
async function triggerUserEvent( | ||
root: Element, | ||
selector: string, | ||
eventNameCamel: string | ||
): Promise<void> { | ||
for (const element of Array.from(root.querySelectorAll(selector))) { | ||
const kebabEventName = fromCamelToKebabCase(eventNameCamel); | ||
const event = { type: kebabEventName }; | ||
const attrName = 'on:' + kebabEventName; | ||
await dispatch(element, attrName, event); | ||
} | ||
await getTestPlatform().flush(); | ||
} | ||
|
||
/** | ||
* CreatePlatfrom and CreateDocument | ||
* @alpha | ||
*/ | ||
export const createDOM = async function () { | ||
const qwik = await getQwik(); | ||
setTestPlatform(qwik.setPlatform); | ||
const host = new ElementFixture().host; | ||
return { | ||
render: function (jsxElement: JSXNode) { | ||
return qwik.render(host, jsxElement); | ||
}, | ||
screen: host, | ||
userEvent: async function (queryOrElement: string | Element | null, eventNameCamel: string) { | ||
if (typeof queryOrElement === 'string') | ||
return triggerUserEvent(host, queryOrElement, eventNameCamel); | ||
const kebabEventName = fromCamelToKebabCase(eventNameCamel); | ||
const event = { type: kebabEventName }; | ||
const attrName = 'on:' + kebabEventName; | ||
await dispatch(queryOrElement, attrName, event); | ||
await getTestPlatform().flush(); | ||
}, | ||
}; | ||
}; | ||
|
||
const getQwik = async (): Promise<typeof import('@builder.io/qwik')> => { | ||
if ((globalThis as any).RUNNER !== false) { | ||
return await import('../core/index'); | ||
} else { | ||
return await import('@builder.io/qwik'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters