Skip to content

Commit 57d704f

Browse files
authored
Merge pull request #507 from nobkd/domino-for-tests
test(nuejs-core): `for`-tests
2 parents aa1e3fa + cfd97d2 commit 57d704f

File tree

8 files changed

+193
-51
lines changed

8 files changed

+193
-51
lines changed

packages/nuejs/test/client/test.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ button:active {
112112

113113
/* grid */
114114
.grid {
115-
grid-template-columns: repeat( auto-fit, minmax(320px, 1fr));
115+
grid-template-columns: repeat(auto-fill, minmax(min(320px, 100%), 1fr));
116116
display: grid;
117117
gap: 2.2em;
118118
}
@@ -122,7 +122,6 @@ button:active {
122122
box-shadow: 1px 1px .3em #ddd;
123123
border-radius: 4px;
124124
padding: 2em;
125-
max-width: 350px;
126125
}
127126

128127

packages/nuejs/test/compile.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
2-
import { join } from 'node:path'
2+
import { join, dirname } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
34

45
import { parse } from '../src/compile.js'
5-
import { getDirname } from './test-utils.js'
66

7-
const srcdir = getDirname(import.meta.url)
7+
const genIndex = (name) => `
8+
<meta charset="utf-8">
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
10+
<link rel="stylesheet" href="../../client/test.css">
11+
12+
<h1>${name}</h1>
13+
14+
<div id="container"></div>
15+
16+
<script type="module">
17+
import createApp from '../../../src/browser/nue.js';
18+
import { lib } from './index.js';
19+
const [App, ...deps] = lib
20+
createApp(App, {}, deps).mount(container)
21+
</script>
22+
`
23+
24+
const srcdir = dirname(fileURLToPath(import.meta.url))
825

926
// Ensure the dist directory exists
1027
const distDir = join(srcdir, 'dist')
@@ -35,5 +52,7 @@ for (const { testPath, name } of testComponentDirs) {
3552
const output = `export const lib = [${components.join(',')}]`
3653
writeFileSync(outputPath, output)
3754

55+
writeFileSync(join(outputDir, 'index.html'), genIndex(name))
56+
3857
console.log(`Compiled ${name}: ${outputPath}`)
3958
}

packages/nuejs/test/test-clicks/index.html

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<main class="grid">
2+
<test-for-array class="card" />
3+
<test-for-array-unpack class="card" />
4+
<test-for-array-numbered class="card" />
5+
<test-for-object-array class="card" />
6+
<test-for-array-replace class="card" />
7+
<test-for-array-funcs class="card" />
8+
</main>
9+
10+
<div @name="test-for-array">
11+
<ul>
12+
<li :for="e in items">{e}</li>
13+
</ul>
14+
15+
<script>
16+
items = ['hello', 'world', 42]
17+
</script>
18+
</div>
19+
20+
<div @name="test-for-array-unpack">
21+
<ul>
22+
<li :for="[e] in items">{e}</li>
23+
</ul>
24+
25+
<script>
26+
items = [['hello', 'world'], [42]]
27+
</script>
28+
</div>
29+
30+
<div @name="test-for-array-numbered">
31+
<ul>
32+
<li :for="e, i in items">{i}: {e}</li>
33+
</ul>
34+
35+
<script>
36+
items = ['hello', 'world', 42]
37+
</script>
38+
</div>
39+
40+
41+
<div @name="test-for-object-array">
42+
<ul>
43+
<li :for="e in items">{e.k}</li>
44+
</ul>
45+
46+
<script>
47+
items = [
48+
{k: 'hello'},
49+
{k: 'world'},
50+
{k: 42},
51+
]
52+
</script>
53+
</div>
54+
55+
<div @name="test-for-array-replace">
56+
<button @click="items[0] == 'hello' ? items = ['world'] : items = ['hello']">Change</button>
57+
<ul>
58+
<li :for="e in items">{e}</li>
59+
</ul>
60+
61+
<script>
62+
items = ['hello']
63+
</script>
64+
</div>
65+
66+
<div @name="test-for-array-funcs">
67+
<div>
68+
<button ref="push" @click="items.push(42)">Push 42</button>
69+
<button ref="pop" @click="items.pop()">Pop</button>
70+
<button ref="unshift" @click="items.unshift('answer')">Unshift 'answer'</button>
71+
<button ref="shift" @click="items.shift()">Shift</button>
72+
<button ref="reverse" @click="items.reverse()">Reverse</button>
73+
<button ref="remove" @click="items.remove('hello')">Remove 'hello'</button>
74+
<button ref="splice" @click="items.splice(1, 1)">Splice second</button>
75+
<button ref="sort" @click="items.sort()">Sort</button>
76+
</div>
77+
78+
<ul>
79+
<li :for="e in items">{e}</li>
80+
</ul>
81+
82+
<script>
83+
items = ['hello', 'world']
84+
</script>
85+
</div>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { mkConfigBase, mountTestComponent } from '../test-utils.js'
2+
3+
const mkConfig = mkConfigBase(import.meta.url)
4+
function arr(parent) { return Array.from(parent.querySelectorAll('li')).map(e => e.textContent) }
5+
6+
7+
describe('Nue.js Fors Tests', () => {
8+
test('Array', async () => {
9+
const { app, cleanup } = await mountTestComponent(mkConfig('test-for-array'))
10+
11+
expect(arr(app.$el)).toEqual(['hello', 'world', '42'])
12+
13+
cleanup()
14+
})
15+
16+
test('Unpacking Array', async () => {
17+
const { app, cleanup } = await mountTestComponent(mkConfig('test-for-array-unpack'))
18+
19+
expect(arr(app.$el)).toEqual(['hello', '42'])
20+
21+
cleanup()
22+
})
23+
24+
test('Numbered Array', async () => {
25+
const { app, cleanup } = await mountTestComponent(mkConfig('test-for-array-numbered'))
26+
27+
expect(arr(app.$el)).toEqual(['0: hello', '1: world', '2: 42'])
28+
29+
cleanup()
30+
})
31+
32+
test('Object Array', async () => {
33+
const { app, cleanup } = await mountTestComponent(mkConfig('test-for-object-array'))
34+
35+
expect(arr(app.$el)).toEqual(['hello', 'world', '42'])
36+
37+
cleanup()
38+
})
39+
40+
test('Array replaced', async () => {
41+
const { app, cleanup } = await mountTestComponent(mkConfig('test-for-array-replace'))
42+
43+
expect(arr(app.$el)).toEqual(['hello'])
44+
app.$el.querySelector('button').click()
45+
expect(arr(app.$el)).toEqual(['world'])
46+
47+
cleanup()
48+
})
49+
50+
test('Array funcs', async () => {
51+
const { app, cleanup } = await mountTestComponent(mkConfig('test-for-array-funcs'))
52+
53+
expect(arr(app.$el)).toEqual(['hello', 'world'])
54+
55+
app.$refs.push.click()
56+
app.$refs.push.click()
57+
expect(arr(app.$el)).toEqual(['hello', 'world', '42', '42'])
58+
59+
app.$refs.pop.click()
60+
expect(arr(app.$el)).toEqual(['hello', 'world', '42'])
61+
62+
app.$refs.unshift.click()
63+
app.$refs.unshift.click()
64+
expect(arr(app.$el)).toEqual(['answer', 'answer', 'hello', 'world', '42'])
65+
66+
app.$refs.shift.click()
67+
expect(arr(app.$el)).toEqual(['answer', 'hello', 'world', '42'])
68+
69+
app.$refs.reverse.click()
70+
expect(arr(app.$el)).toEqual(['42', 'world', 'hello', 'answer'])
71+
72+
app.$refs.remove.click()
73+
expect(arr(app.$el)).toEqual(['42', 'world', 'answer'])
74+
75+
app.$refs.splice.click()
76+
expect(arr(app.$el)).toEqual(['42', 'answer'])
77+
78+
app.$refs.push.click() // additional 42 at end for sort
79+
app.$refs.sort.click()
80+
expect(arr(app.$el)).toEqual(['42', '42', 'answer'])
81+
82+
cleanup()
83+
})
84+
})

packages/nuejs/test/test-ifs/index.html

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/nuejs/test/test-refs/index.html

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/nuejs/test/test-utils.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ import { createWindow } from 'domino'
77
import { parse } from '../src/compile.js'
88

99

10-
/**
11-
* Get directory from url
12-
* @param {Url} url - Pass `import.meta.url`
13-
* @returns {string} Directory path containing the current file
14-
*/
15-
export function getDirname(url) {
16-
return dirname(fileURLToPath(url))
17-
}
18-
1910
/**
2011
* Get directory path from url and return function for usage with {@linkcode mountTestComponent}
2112
* @param {Url} url - `import.meta.url` to get the test directory
@@ -25,7 +16,7 @@ export function mkConfigBase(url) {
2516
* @param {string} componentName
2617
* @param {Object?} data
2718
*/
28-
return (componentName, data) => ({ testPath: getDirname(url), componentName, data })
19+
return (componentName, data) => ({ testPath: dirname(fileURLToPath(url)), componentName, data })
2920
}
3021

3122

0 commit comments

Comments
 (0)