1 | const test = require('ava')
|
2 | const R = require('ramda')
|
3 | const os = require('os')
|
4 | const {
|
5 | findPackageJson,
|
6 | getConfig,
|
7 | DEFAULT_CONFIG,
|
8 | PACKAGE_JSON_CONFIG_KEY,
|
9 | generateScriptsForPackageJson,
|
10 | generateConfigForPackageJson,
|
11 | } = require('../lib/config')
|
12 | const overriddenConfig = require('./config.json')
|
13 |
|
14 | test(`findPackageJson should return nearest path to package.json`, (t) => {
|
15 | const actual = findPackageJson(__dirname)
|
16 | const expected = R.init(__dirname.split('/'))
|
17 | .concat('package.json')
|
18 | .join('/')
|
19 | t.is(actual, expected)
|
20 | })
|
21 |
|
22 | test(`findPackageJson should return null if nearest package.json not found`, (t) => {
|
23 | const actual = findPackageJson(os.homedir())
|
24 | const expected = null
|
25 | t.is(actual, expected)
|
26 | })
|
27 |
|
28 | test(`getConfig should return default config if package.json not found`, (t) => {
|
29 | const configPath = null
|
30 | const actual = getConfig(configPath)
|
31 | t.deepEqual(actual, DEFAULT_CONFIG)
|
32 | })
|
33 |
|
34 | test(`getConfig should return default config overridden by config.json + projectRoot`, (t) => {
|
35 | const configPath = `${__dirname}/config.json`
|
36 | const actual = getConfig(configPath)
|
37 | const projectRoot = __dirname
|
38 | const expected = Object.assign({}, DEFAULT_CONFIG, overriddenConfig[PACKAGE_JSON_CONFIG_KEY], { projectRoot })
|
39 | t.deepEqual(actual, expected)
|
40 | })
|
41 |
|
42 | test('getConfig should return DEFAULT_CONFIG if configPath is incorrect', (t) => {
|
43 | const configPath = `${__dirname}/package.json`
|
44 | const actual = getConfig(configPath)
|
45 | const expected = DEFAULT_CONFIG
|
46 | t.is(actual, expected)
|
47 | })
|
48 |
|
49 | test('generateScriptsForPackageJson', (t) => {
|
50 | const actual = generateScriptsForPackageJson()
|
51 | const expected = ` "scripts": {
|
52 | ...
|
53 | "spdt:generate-story-index": "./node_modules/.bin/spdt:generate-story-index",
|
54 | "spdt:generate-test-index": "./node_modules/.bin/spdt:generate-test-index",
|
55 | "spdt:generate-tests": "./node_modules/.bin/spdt:generate-tests",
|
56 | "spdt:test": "jest --detectOpenHandles --config ./.spdt/jest.spdt.config.js",
|
57 | "spdt:test:chrome": "HEADLESS=false jest --detectOpenHandles --config ./.spdt/jest.spdt.config.js",
|
58 | "spdt:test:chrome:slow": "SLOWMO=1000 HEADLESS=false jest --detectOpenHandles --config ./.spdt/jest.spdt.config.js",
|
59 | "spdt:storybook": "start-storybook -p 9009 -c ./.spdt",
|
60 | "spdt": "npm run spdt:generate-story-index && npm run spdt:generate-test-index && npm run spdt:generate-tests && npm run spdt:storybook"
|
61 | }`
|
62 | t.is(actual, expected)
|
63 | })
|
64 | test('generateConfigForPackageJson', (t) => {
|
65 | const actual = generateConfigForPackageJson()
|
66 | const expected = ` "${PACKAGE_JSON_CONFIG_KEY}": {
|
67 | "pathToSrc": "./src",
|
68 | "pathToStories": "./.spdt",
|
69 | "pathToTestIndex": "./.spdt",
|
70 | "testIndexName": "test-index.generated.js"
|
71 | }`
|
72 | t.is(actual, expected)
|
73 | })
|