-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.test.js
More file actions
78 lines (71 loc) · 2.94 KB
/
Copy pathslice.test.js
File metadata and controls
78 lines (71 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'fs/promises';
import path from 'path';
import os from 'os';
import { generateSlice } from './slice.js';
async function inTempDir(fn) {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'semantic-react-'));
const cwd = process.cwd();
process.chdir(dir);
try {
await fn(dir);
} finally {
process.chdir(cwd);
await fs.rm(dir, { recursive: true, force: true });
}
}
test('creates a slice file with createSlice boilerplate', async () => {
await inTempDir(async (dir) => {
await generateSlice('counter', { ts: true });
const base = path.join(dir, 'slices');
const ts = await fs.readFile(path.join(base, 'counter.ts'), 'utf8');
assert.match(ts, /import \{ createSlice \} from '@reduxjs\/toolkit';/);
assert.match(ts, /const counterSlice = createSlice\(\{/);
assert.match(ts, /name: 'counter',/);
assert.match(ts, /export default counterSlice\.reducer;/);
});
});
test('defaults to a .js extension when no option is passed', async () => {
await inTempDir(async (dir) => {
await generateSlice('user');
const base = path.join(dir, 'slices');
await fs.access(path.join(base, 'user.js'));
});
});
test('switching extension removes the stale file when confirmed', async () => {
await inTempDir(async (dir) => {
await generateSlice('cart', { jsx: true });
await generateSlice('cart', { tsx: true }, undefined, async () => true);
const base = path.join(dir, 'slices');
await fs.access(path.join(base, 'cart.tsx'));
await assert.rejects(fs.access(path.join(base, 'cart.jsx')));
});
});
test('switching extension keeps the stale file when not confirmed', async () => {
await inTempDir(async (dir) => {
await generateSlice('auth', { jsx: true });
await generateSlice('auth', { tsx: true }, undefined, async () => false);
const base = path.join(dir, 'slices');
await fs.access(path.join(base, 'auth.tsx'));
await fs.access(path.join(base, 'auth.jsx'));
});
});
test('scaffolds a reducer function for each name passed', async () => {
await inTempDir(async (dir) => {
await generateSlice('counter', { ts: true }, undefined, undefined, ['increment', 'decrement']);
const base = path.join(dir, 'slices');
const ts = await fs.readFile(path.join(base, 'counter.ts'), 'utf8');
assert.match(ts, /increment\(state\) \{\n\s+\/\/ \.\.\.\n\s+\},/);
assert.match(ts, /decrement\(state\) \{\n\s+\/\/ \.\.\.\n\s+\},/);
assert.match(ts, /export const \{ increment, decrement \} = counterSlice\.actions;/);
});
});
test('writes into a custom destination path when one is given', async () => {
await inTempDir(async (dir) => {
await generateSlice('todos', { ts: true }, 'src/store/slices');
const base = path.join(dir, 'src', 'store', 'slices');
const ts = await fs.readFile(path.join(base, 'todos.ts'), 'utf8');
assert.match(ts, /export default todosSlice\.reducer;/);
});
});