Skip to content

Commit 762b60a

Browse files
authored
test: add additional tests for different implementations (#20)
* chore: silence vercel comments * test: add separate tests for different xstate implementations * test: add requirement for machine to be defined
1 parent 3e2d73b commit 762b60a

File tree

12 files changed

+120
-29
lines changed

12 files changed

+120
-29
lines changed

Diff for: tests/e2e/minimal.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import { fileURLToPath } from 'node:url'
5+
import { describe } from 'vitest'
6+
import { setup } from '@nuxt/test-utils'
7+
8+
import runTests from './tests'
9+
10+
describe('Minimal implementation of the module', async () => {
11+
await setup({
12+
server: true,
13+
browser: true,
14+
rootDir: fileURLToPath(new URL('../fixture/minimal', import.meta.url))
15+
})
16+
17+
runTests()
18+
})

Diff for: tests/e2e/regular.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import { fileURLToPath } from 'node:url'
5+
import { describe } from 'vitest'
6+
import { setup } from '@nuxt/test-utils'
7+
8+
import runTests from './tests'
9+
10+
describe('Regular implementation of the module', async () => {
11+
await setup({
12+
server: true,
13+
browser: true,
14+
rootDir: fileURLToPath(new URL('../fixture/regular', import.meta.url))
15+
})
16+
17+
runTests()
18+
})
+13-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
/**
2-
* @vitest-environment node
3-
*/
4-
import { fileURLToPath } from 'node:url'
5-
import { describe, test, expect } from 'vitest'
6-
import { setup, createPage } from '@nuxt/test-utils'
7-
8-
describe('ssr', async () => {
9-
await setup({
10-
server: true,
11-
browser: true,
12-
rootDir: fileURLToPath(new URL('../fixture', import.meta.url))
13-
})
1+
import { test, expect } from 'vitest'
2+
import { createPage } from '@nuxt/test-utils'
143

4+
export default function () {
155
test('shows proper state before clicking', async () => {
166
const page = await createPage('/')
177

@@ -28,7 +18,13 @@ describe('ssr', async () => {
2818
expect(stateText).toBe('loading')
2919
})
3020

31-
// test('auto-imports state machine correctly', () => {
32-
// expect(1 + 1).toBe(2)
33-
// })
34-
})
21+
test('auto-imported state machine works correctly', async () => {
22+
const page = await createPage('/')
23+
24+
const machineString = await page.innerText('[data-test-id="machine"]')
25+
const machine = JSON.parse(machineString)
26+
27+
// TODO: Add more (possibly complex) test cases
28+
expect(machine.state).not.toBe(undefined)
29+
})
30+
}

Diff for: tests/fixture/app.vue renamed to tests/fixture/minimal/app.vue

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
<button data-test-id="click-btn" @click="send('CLICK')">
55
CLICK
66
</button>
7+
<p data-test-id="machine">
8+
{{ machine }}
9+
</p>
710
</div>
811
</template>
912

1013
<script setup>
11-
const { state, send } = useMachine(loadingMachine)
14+
const machine = useMachine(loadingMachine)
15+
const { state, send } = machine
1216
</script>
File renamed without changes.

Diff for: tests/fixture/minimal/nuxt.config.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineNuxtConfig } from 'nuxt/config'
2+
import XStateModule from '../../../'
3+
4+
// Fixture module used mostly for testing when using the minimal option
5+
export default defineNuxtConfig({
6+
modules: [
7+
// @ts-ignore-next-line
8+
XStateModule
9+
],
10+
xState: {
11+
minimal: true
12+
}
13+
})

Diff for: tests/fixture/nuxt.config.ts

-9
This file was deleted.

Diff for: tests/fixture/regular/app.vue

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<template>
2+
<div>
3+
<h1>Current state: <span data-test-id="state-txt">{{ state.value }}</span></h1>
4+
<button data-test-id="click-btn" @click="send('CLICK')">
5+
CLICK
6+
</button>
7+
<p data-test-id="machine">
8+
{{ machine }}
9+
</p>
10+
</div>
11+
</template>
12+
13+
<script setup>
14+
const machine = useMachine(loadingMachine)
15+
const { state, send } = machine
16+
</script>

Diff for: tests/fixture/regular/machines/loading.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default createMachine({
2+
id: 'loading',
3+
initial: 'idle',
4+
predictableActionArguments: true,
5+
states: {
6+
idle: {
7+
on: {
8+
CLICK: 'loading'
9+
}
10+
},
11+
loading: {
12+
on: {
13+
CLICK: 'idle'
14+
}
15+
}
16+
}
17+
})

Diff for: tests/fixture/regular/nuxt.config.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineNuxtConfig } from 'nuxt/config'
2+
import XStateModule from '../../../'
3+
4+
// Fixture module used mostly for testing when using the minimal option
5+
export default defineNuxtConfig({
6+
modules: [
7+
// @ts-ignore-next-line
8+
XStateModule
9+
],
10+
xState: {
11+
minimal: false
12+
}
13+
})

Diff for: vercel.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"github": {
3+
"silent": true
4+
}
5+
}

Diff for: vitest.config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { defineConfig } from 'vitest/config'
44
export default defineConfig({
55
test: {
66
deps: {
7-
inline: [/@nuxt\/test-utils-edge/]
7+
inline: [/@nuxt\/test-utils/]
88
},
9-
testTimeout: 10000
9+
testTimeout: 15000
1010
}
1111
})

0 commit comments

Comments
 (0)