js-build-tools 1.2.3 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +573 -250
  2. package/dist/build.js +27 -20
  3. package/dist/build.min.js +1 -1
  4. package/dist/bundle.js +16 -13
  5. package/dist/bundle.min.js +1 -1
  6. package/dist/bundleLint.js +22 -18
  7. package/dist/bundleLint.min.js +1 -1
  8. package/dist/bundleMinify.js +13 -17
  9. package/dist/bundleMinify.min.js +1 -1
  10. package/dist/compileReadme.js +13 -11
  11. package/dist/compileReadme.min.js +1 -1
  12. package/dist/default.js +17 -12
  13. package/dist/default.min.js +1 -1
  14. package/dist/dist.js +11 -7
  15. package/dist/dist.min.js +1 -1
  16. package/dist/distLint.js +20 -17
  17. package/dist/distLint.min.js +1 -1
  18. package/dist/distMinify.js +12 -17
  19. package/dist/distMinify.min.js +1 -1
  20. package/dist/partials/addToReadme.js +25 -0
  21. package/dist/partials/beginWatcher.js +16 -0
  22. package/dist/partials/clean.js +21 -0
  23. package/dist/partials/distFor.js +23 -0
  24. package/dist/partials/distForSrc.js +16 -0
  25. package/dist/partials/distSeries.js +31 -0
  26. package/dist/partials/minifyFor.js +21 -0
  27. package/dist/partials/readmeTemplate.js +18 -0
  28. package/dist/partials/runOnChange.js +63 -0
  29. package/dist/partials/tsFor.js +24 -0
  30. package/dist/partials.js +33 -0
  31. package/dist/partials.min.js +1 -0
  32. package/dist/test-helpers/circularObject.js +50 -0
  33. package/dist/test-helpers/countMatches.js +16 -0
  34. package/dist/test-helpers/deepReferenceObject.js +20 -0
  35. package/dist/test-helpers/domItem.js +33 -0
  36. package/dist/test-helpers/jsonDom.js +20 -0
  37. package/dist/test-helpers/linkedList.js +23 -0
  38. package/dist/test-helpers/logObject.js +24 -0
  39. package/dist/test-helpers/multiReferenceObject.js +20 -0
  40. package/dist/test-helpers/nodeTree.js +28 -0
  41. package/dist/test-helpers/setUp.js +127 -0
  42. package/dist/testFull.js +21 -10
  43. package/dist/testFull.min.js +1 -1
  44. package/dist/testHelpers.js +33 -198
  45. package/dist/testHelpers.min.js +1 -1
  46. package/dist/testQuick.js +23 -12
  47. package/dist/testQuick.min.js +1 -1
  48. package/dist/typeScript.js +14 -0
  49. package/dist/typeScript.min.js +1 -0
  50. package/dist/watchFull.js +14 -21
  51. package/dist/watchFull.min.js +1 -1
  52. package/dist/watchTest.js +16 -14
  53. package/dist/watchTest.min.js +1 -1
  54. package/functions/build.js +8 -5
  55. package/functions/build.test.js +59 -0
  56. package/functions/bundle.js +6 -4
  57. package/functions/bundle.test.js +68 -0
  58. package/functions/bundleLint.js +5 -3
  59. package/functions/bundleLint.test.js +61 -0
  60. package/functions/bundleMinify.js +4 -7
  61. package/functions/bundleMinify.test.js +14 -0
  62. package/functions/compileReadme.js +3 -2
  63. package/functions/compileReadme.test.js +18 -0
  64. package/functions/default.js +5 -2
  65. package/functions/default.test.js +32 -0
  66. package/functions/dist.js +3 -1
  67. package/functions/dist.test.js +43 -0
  68. package/functions/distLint.js +4 -2
  69. package/functions/distLint.test.js +58 -0
  70. package/functions/distMinify.js +4 -7
  71. package/functions/distMinify.test.js +14 -0
  72. package/functions/partials/addToReadme.js +20 -0
  73. package/functions/partials/addToReadme.test.js +28 -0
  74. package/functions/partials/beginWatcher.js +12 -0
  75. package/functions/partials/beginWatcher.test.js +18 -0
  76. package/functions/partials/clean.js +14 -0
  77. package/functions/partials/clean.test.js +20 -0
  78. package/functions/partials/distFor.js +18 -0
  79. package/functions/partials/distFor.test.js +125 -0
  80. package/functions/partials/distForSrc.js +14 -0
  81. package/functions/partials/distForSrc.test.js +17 -0
  82. package/functions/partials/distSeries.js +21 -0
  83. package/functions/partials/distSeries.test.js +33 -0
  84. package/functions/partials/minifyFor.js +16 -0
  85. package/functions/partials/minifyFor.test.js +48 -0
  86. package/functions/partials/readmeTemplate.js +15 -0
  87. package/functions/partials/readmeTemplate.test.js +33 -0
  88. package/functions/partials/runOnChange.js +57 -0
  89. package/functions/partials/runOnChange.test.js +93 -0
  90. package/functions/partials/tsFor.js +21 -0
  91. package/functions/partials/tsFor.test.js +44 -0
  92. package/functions/partials.js +32 -0
  93. package/functions/partials.test.js +7 -0
  94. package/functions/test-helpers/circularObject.js +28 -0
  95. package/functions/test-helpers/circularObject.test.js +14 -0
  96. package/functions/test-helpers/countMatches.js +11 -0
  97. package/functions/test-helpers/countMatches.test.js +9 -0
  98. package/functions/test-helpers/deepReferenceObject.js +25 -0
  99. package/functions/test-helpers/deepReferenceObject.test.js +7 -0
  100. package/functions/test-helpers/domItem.js +31 -0
  101. package/functions/test-helpers/domItem.test.js +12 -0
  102. package/functions/test-helpers/jsonDom.js +16 -0
  103. package/functions/test-helpers/jsonDom.test.js +12 -0
  104. package/functions/test-helpers/linkedList.js +10 -0
  105. package/functions/test-helpers/linkedList.test.js +22 -0
  106. package/functions/test-helpers/logObject.js +18 -0
  107. package/functions/test-helpers/logObject.test.js +20 -0
  108. package/functions/test-helpers/multiReferenceObject.js +25 -0
  109. package/functions/test-helpers/multiReferenceObject.test.js +14 -0
  110. package/functions/test-helpers/nodeTree.js +11 -0
  111. package/functions/test-helpers/nodeTree.test.js +18 -0
  112. package/functions/test-helpers/setUp.js +67 -0
  113. package/functions/test-helpers/setUp.test.js +18 -0
  114. package/functions/testFull.js +13 -3
  115. package/functions/testFull.test.js +16 -0
  116. package/functions/testHelpers.js +27 -137
  117. package/functions/testHelpers.test.js +3 -96
  118. package/functions/testQuick.js +12 -2
  119. package/functions/testQuick.test.js +16 -0
  120. package/functions/typeScript.js +11 -0
  121. package/functions/typeScript.test.js +10 -0
  122. package/functions/watchFull.js +6 -12
  123. package/functions/watchFull.test.js +40 -0
  124. package/functions/watchTest.js +4 -2
  125. package/functions/watchTest.test.js +48 -0
  126. package/gulp.config.js +161 -16
  127. package/gulpfile.base.js +9 -0
  128. package/package.json +2 -1
  129. package/dist/addToReadme.js +0 -22
  130. package/dist/addToReadme.min.js +0 -1
  131. package/dist/clean.js +0 -11
  132. package/dist/clean.min.js +0 -1
  133. package/dist/distFor.js +0 -21
  134. package/dist/distFor.min.js +0 -1
  135. package/dist/readmeTemplate.js +0 -15
  136. package/dist/readmeTemplate.min.js +0 -1
  137. package/functions/addToReadme.js +0 -17
  138. package/functions/clean.js +0 -10
  139. package/functions/distFor.js +0 -15
  140. package/functions/readmeTemplate.js +0 -13
@@ -0,0 +1,48 @@
1
+ const fs = require('fs')
2
+ const setUp = require('../test-helpers/setUp')
3
+ setUp.setDefaults('test-minify-for')
4
+ const gulpConfig = setUp.gulpConfig
5
+ const minifyFor = require('./minifyFor')
6
+ const { countMatches } = require('../testHelpers')
7
+
8
+ const rawContents = 'const { dest, src } = require(\'gulp\')\n' +
9
+ 'const { default: uglify } = require(\'gulp-uglify-es\')\n' +
10
+ 'const rename = require(\'gulp-rename\')\n' +
11
+ '\n' +
12
+ '/**\n' +
13
+ ' * Minify files and rename the output with \'.min\' extension.\n' +
14
+ ' * @returns {*}\n' +
15
+ ' */\n' +
16
+ 'const minifyFor = (srcSearch, destination) => src(srcSearch)\n' +
17
+ ' .pipe(uglify())\n' +
18
+ ' .pipe(rename({ extname: \'.min.js\' }))\n' +
19
+ ' .pipe(dest(destination))\n' +
20
+ '\n' +
21
+ 'module.exports = minifyFor\n'
22
+
23
+ beforeEach(() => setUp.beforeEach()
24
+ .then(() => fs.writeFileSync(gulpConfig.get('srcPath') + '/minifyFor.js', rawContents)))
25
+
26
+ afterEach(setUp.afterEach)
27
+
28
+ describe('minifyFor', () => {
29
+ test('copies the src directory and minifies it into the dist directory with min extension', done => {
30
+ const srcPath = gulpConfig.get('srcPath')
31
+ const filePath = `${srcPath}/minifyFor.js`
32
+ const expectedFilePath = `${srcPath}/minifyFor.min.js`
33
+ expect.assertions(3)
34
+ const oldContents = fs.readFileSync(filePath).toString()
35
+ expect(countMatches(oldContents, '\n')).toBe(14)
36
+ minifyFor(filePath, srcPath)
37
+ .on('finish', () => {
38
+ expect(fs.existsSync(expectedFilePath)).toBeTruthy()
39
+ const minifiedContents = fs.readFileSync(expectedFilePath).toString()
40
+ expect(countMatches(minifiedContents, '\n')).toBe(0)
41
+ done()
42
+ })
43
+ .on('error', error => {
44
+ console.error('Encountered error', error)
45
+ done()
46
+ })
47
+ })
48
+ })
@@ -0,0 +1,15 @@
1
+ const { dest, src } = require('gulp')
2
+ const gulpConfig = require('../../gulp.config.js')
3
+ const rename = require('gulp-rename')
4
+
5
+ /**
6
+ * Copy a readme template into the README.md file.
7
+ * @function
8
+ * @memberOf module:partials
9
+ * @returns {*}
10
+ */
11
+ const readmeTemplate = () => src(gulpConfig.get('readmeTemplate'))
12
+ .pipe(rename(gulpConfig.get('readmeFile')))
13
+ .pipe(dest(gulpConfig.get('readmePath')))
14
+
15
+ module.exports = readmeTemplate
@@ -0,0 +1,33 @@
1
+ const fs = require('fs')
2
+ const setUp = require('../test-helpers/setUp')
3
+ setUp.setDefaults('test-readme-template')
4
+ const gulpConfig = setUp.gulpConfig
5
+ const readmeTemplate = require('./readmeTemplate')
6
+
7
+ const mainMd = 'MAIN.md'
8
+ const mainTestPath = gulpConfig.get('readmeTemplate')
9
+
10
+ beforeEach(() => setUp.beforeEach()
11
+ .then(() => fs.copyFileSync(mainMd, mainTestPath)))
12
+
13
+ afterEach(setUp.afterEach)
14
+
15
+ describe('readmeTemplate', () => {
16
+ test('copies the main contents into the new README.md', done => {
17
+ const readmeFilePath = gulpConfig.get('readmePath') + gulpConfig.get('readmeFile')
18
+ const mainContents = fs.readFileSync(mainTestPath)
19
+ expect.assertions(2)
20
+ readmeTemplate()
21
+ .on('finish', async () => {
22
+ await expect(fs.existsSync(readmeFilePath)).toBeTruthy()
23
+ const readme = await fs.readFileSync(readmeFilePath)
24
+ // Confirm that the contents for MAIN.md are copied to README.md
25
+ await expect(readme).toEqual(mainContents)
26
+ done()
27
+ })
28
+ .on('error', error => {
29
+ console.error('Encountered error', error)
30
+ done()
31
+ })
32
+ })
33
+ })
@@ -0,0 +1,57 @@
1
+ const gulpConfig = require('../../gulp.config.js')
2
+ const bundle = require('../bundle')
3
+ const distSeries = require('./distSeries')
4
+ const { parallel, series } = require('gulp')
5
+ const testQuick = require('../testQuick')
6
+
7
+ /**
8
+ * Run this function when the watched files are modified.
9
+ * 1. Find the sub-folders within src path
10
+ * 2. Maintain the folders, but use distPath for base
11
+ * 3. Remove base folder and return dist path with correct sub-folders
12
+ * @example
13
+ * // Configured paths
14
+ * distPath = 'dist'
15
+ * srcPath = 'functions'
16
+ *
17
+ * // Path parameter
18
+ * path = 'functions/some/path/file.js'
19
+ *
20
+ * // Generated regex using configured srcPath
21
+ * pathRegex = '/^functions(.*\/).+\.js$/i'
22
+ *
23
+ * // Replace value using the configured distPath
24
+ * replacePath = 'dist$1'
25
+ *
26
+ * // The resulting replaced path for the destination folder
27
+ * distPathResult = 'dist/some/path/'
28
+ * @function
29
+ * @memberOf module:partials
30
+ * @param {string} path
31
+ * @returns {stream.Stream}
32
+ */
33
+ const runOnChange = path => {
34
+ const useTs = gulpConfig.get('useTsConfig')
35
+ const distPath = gulpConfig.get('distPath')
36
+ const srcPath = gulpConfig.get('srcPath')
37
+ /**
38
+ * 1. The original path comes in from src and is a .ts
39
+ * 2. Discover the outgoing dist path where that file should go
40
+ * 3. Use the path and dist in tsFor
41
+ * 4. Take the original path, convert to full file path in dist
42
+ * 5. Use the dist path found previously in #2
43
+ * 6. Use the full dist path and the dist outgoing path in distFor
44
+ */
45
+ const pathRegex = new RegExp(`^${srcPath}(.*\\/)(.+)\\.(js|ts)$`, 'i')
46
+ const distPathResult = path.replace(pathRegex, `${distPath}$1`)
47
+ let distSrcPath = path
48
+ if (useTs) {
49
+ distSrcPath = path.replace(pathRegex, `${distPath}$1$2.js`)
50
+ }
51
+ const runSeries = distSeries(distSrcPath, distPathResult, path)
52
+ return gulpConfig.get('nodeOnly')
53
+ ? series(testQuick, runSeries)()
54
+ : parallel(testQuick, series(runSeries, bundle))()
55
+ }
56
+
57
+ module.exports = runOnChange
@@ -0,0 +1,93 @@
1
+ const fs = require('fs')
2
+ const setUp = require('../test-helpers/setUp')
3
+ setUp.setDefaults('test-run-on-change')
4
+ const gulpConfig = setUp.gulpConfig
5
+ const bundle = require('../bundle')
6
+ const distFor = require('./distFor')
7
+ const runOnChange = require('./runOnChange')
8
+ const tsFor = require('./tsFor')
9
+ const testQuick = require('../testQuick')
10
+
11
+ const genericFunction = () => Promise.resolve(true)
12
+
13
+ jest.mock('../bundle', () => jest.fn(genericFunction))
14
+ jest.mock('./distFor', () => jest.fn(genericFunction))
15
+ jest.mock('./tsFor', () => jest.fn(genericFunction))
16
+ jest.mock('../testQuick', () => jest.fn(genericFunction))
17
+
18
+ const rawContents = 'export function sayHello(name: string) {\n' +
19
+ ' return `Hello from ${name}`;\n' +
20
+ '}'
21
+
22
+ const distPath = gulpConfig.get('distPath') + '/'
23
+ const srcPath = gulpConfig.get('srcPath')
24
+ const srcFile = `${srcPath}/runOnChange.js`
25
+ const srcTsFile = `${srcPath}/runOnChange.ts`
26
+
27
+ beforeEach(() => {
28
+ return setUp.beforeEach()
29
+ .then(() => fs.writeFileSync(srcTsFile, rawContents))
30
+ .then(() => fs.writeFileSync(srcFile, rawContents))
31
+ }
32
+ )
33
+
34
+ afterEach(() => {
35
+ jest.clearAllMocks()
36
+ return setUp.afterEach()
37
+ })
38
+
39
+ describe('runOnChange', () => {
40
+ test('runs testQuick and distFor for nodeOnly true and useTs false', done => {
41
+ gulpConfig.set('nodeOnly', true)
42
+ gulpConfig.set('useTsConfig', false)
43
+ expect.assertions(2)
44
+ runOnChange(srcFile)
45
+ setTimeout(() => {
46
+ expect(testQuick).toHaveBeenCalled()
47
+ expect(distFor).toHaveBeenCalledWith(srcFile, distPath)
48
+ done()
49
+ }, 500)
50
+ })
51
+
52
+ test('runs testQuick and distFor for nodeOnly true and tsFor for useTs true', done => {
53
+ gulpConfig.set('nodeOnly', true)
54
+ gulpConfig.set('useTsConfig', true)
55
+ const distSrcPath = distPath + 'runOnChange.js'
56
+ expect.assertions(3)
57
+ runOnChange(srcTsFile)
58
+ setTimeout(() => {
59
+ expect(testQuick).toHaveBeenCalled()
60
+ expect(tsFor).toHaveBeenCalledWith(srcTsFile, distPath)
61
+ expect(distFor).toHaveBeenCalledWith(distSrcPath, distPath)
62
+ done()
63
+ }, 500)
64
+ })
65
+
66
+ test('runs testQuick and distFor then bundle for nodeOnly false and useTs false', done => {
67
+ gulpConfig.set('nodeOnly', false)
68
+ gulpConfig.set('useTsConfig', false)
69
+ expect.assertions(3)
70
+ runOnChange(srcFile)
71
+ setTimeout(() => {
72
+ expect(testQuick).toHaveBeenCalled()
73
+ expect(distFor).toHaveBeenCalledWith(srcFile, distPath)
74
+ expect(bundle).toHaveBeenCalled()
75
+ done()
76
+ }, 500)
77
+ })
78
+
79
+ test('runs testQuick and distFor then bundle for nodeOnly false and tsFor for useTs true', done => {
80
+ gulpConfig.set('nodeOnly', false)
81
+ gulpConfig.set('useTsConfig', true)
82
+ const distSrcPath = distPath + 'runOnChange.js'
83
+ expect.assertions(4)
84
+ runOnChange(srcTsFile)
85
+ setTimeout(() => {
86
+ expect(testQuick).toHaveBeenCalled()
87
+ expect(tsFor).toHaveBeenCalledWith(srcTsFile, distPath)
88
+ expect(distFor).toHaveBeenCalledWith(distSrcPath, distPath)
89
+ expect(bundle).toHaveBeenCalled()
90
+ done()
91
+ }, 500)
92
+ })
93
+ })
@@ -0,0 +1,21 @@
1
+ const { dest, src } = require('gulp')
2
+ const gulpConfig = require('../../gulp.config.js')
3
+ const ts = require('gulp-typescript')
4
+
5
+ const tsProject = ts.createProject(gulpConfig.get('useTsConfig'))
6
+
7
+ /**
8
+ * Starting at the source directory, find all the ts files and convert them into the distribution directory.
9
+ * @function
10
+ * @memberOf module:partials
11
+ * @param {string|array} [srcPath='']
12
+ * @param {string} [distPath='']
13
+ * @returns {stream.Stream}
14
+ * @see `https://www.typescriptlang.org/docs/handbook/gulp.html` for more info
15
+ */
16
+ const tsFor = (srcPath = gulpConfig.get('tsSearch'), distPath = gulpConfig.get('distPath')) => src(srcPath)
17
+ .pipe(tsProject())
18
+ .js
19
+ .pipe(dest(distPath))
20
+
21
+ module.exports = tsFor
@@ -0,0 +1,44 @@
1
+ const fs = require('fs')
2
+ const setUp = require('../test-helpers/setUp')
3
+ setUp.setDefaults('test-ts-for')
4
+ const gulpConfig = setUp.gulpConfig
5
+ const tsFor = require('./tsFor')
6
+ const { countMatches, logObject } = require('../testHelpers')
7
+
8
+ const rawContents = 'export function sayHello(name: string) {\n' +
9
+ ' return `Hello from ${name}`;\n' +
10
+ '}'
11
+
12
+ beforeEach(
13
+ () => setUp.beforeEach()
14
+ .then(
15
+ async () => {
16
+ const srcPath = gulpConfig.get('srcPath')
17
+ return fs.writeFileSync(`${srcPath}/typeScript.ts`, rawContents)
18
+ }
19
+ )
20
+ )
21
+
22
+ afterEach(setUp.afterEach)
23
+
24
+ describe('tsFor', () => {
25
+ test('copies the src directory and compiles it into the dist directory', done => {
26
+ const distPath = gulpConfig.get('distPath')
27
+ const srcPath = gulpConfig.get('srcPath')
28
+ const srcFile = `${srcPath}/typeScript.ts`
29
+ expect.assertions(3)
30
+ const oldContents = fs.readFileSync(srcFile).toString()
31
+ expect(countMatches(oldContents, 'sayHello(name: string)')).toEqual(1)
32
+ tsFor(srcFile, distPath)
33
+ .on('finish', () => {
34
+ expect(fs.existsSync(distPath)).toBeTruthy()
35
+ const compiledContents = fs.readFileSync(`${distPath}/typeScript.js`).toString()
36
+ expect(countMatches(compiledContents, 'sayHello(name)')).toEqual(1)
37
+ done()
38
+ })
39
+ .on('error', error => {
40
+ console.error('Encountered error', error)
41
+ done()
42
+ })
43
+ }, 30000)
44
+ })
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Micro-functions used as components for the main gulp functions.
3
+ * @file
4
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
5
+ * @version 2.0.0
6
+ * @module partials
7
+ * @memberOf module:js-build-tools
8
+ */
9
+
10
+ const addToReadme = require('./partials/addToReadme')
11
+ const beginWatcher = require('./partials/beginWatcher')
12
+ const clean = require('./partials/clean')
13
+ const distFor = require('./partials/distFor')
14
+ const distForSrc = require('./partials/distForSrc')
15
+ const distSeries = require('./partials/distSeries')
16
+ const minifyFor = require('./partials/minifyFor')
17
+ const readmeTemplate = require('./partials/readmeTemplate')
18
+ const runOnChange = require('./partials/runOnChange')
19
+ const tsFor = require('./partials/tsFor')
20
+
21
+ module.exports = {
22
+ addToReadme,
23
+ beginWatcher,
24
+ clean,
25
+ distFor,
26
+ distForSrc,
27
+ distSeries,
28
+ minifyFor,
29
+ readmeTemplate,
30
+ runOnChange,
31
+ tsFor,
32
+ }
@@ -0,0 +1,7 @@
1
+ const partials = require('./partials')
2
+
3
+ describe('partials', () => {
4
+ test('is object with all partial exports', () => {
5
+ expect(Object.keys(partials).length).toBe(10)
6
+ })
7
+ })
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Multilayered node tree-like structure with parent references
3
+ * @memberOf module:testHelpers
4
+ * @type {Object.<string, string|Object|Array>}
5
+ */
6
+ const circularObject = { name: 'root', parent: {}, body: {}, head: {}, children: [] }
7
+ circularObject.children = [
8
+ { name: 'body', parent: {}, children: [] },
9
+ { name: 'head', parent: {}, children: [] }
10
+ ]
11
+ circularObject.body = circularObject.children[0]
12
+ circularObject.head = circularObject.children[1]
13
+ circularObject.body.parent = circularObject
14
+ circularObject.head.parent = circularObject
15
+ circularObject.body.children = [
16
+ { name: 'body child one', parent: {}, children: [] },
17
+ { name: 'body child two', parent: {}, children: [] }
18
+ ]
19
+ circularObject.body.children[0].parent = circularObject.body
20
+ circularObject.body.children[1].parent = circularObject.body
21
+ circularObject.head.children = [
22
+ { name: 'head child one', parent: {}, children: [] },
23
+ { name: 'head child two', parent: {}, children: [] }
24
+ ]
25
+ circularObject.head.children[0].parent = circularObject.head
26
+ circularObject.head.children[1].parent = circularObject.head
27
+
28
+ module.exports = circularObject
@@ -0,0 +1,14 @@
1
+ import circularObject from './circularObject'
2
+
3
+ describe('circularObject', () => {
4
+ test('has sample circular object', () => {
5
+ expect(circularObject.body.parent).toBe(circularObject)
6
+ expect(circularObject.body.children[0].parent).toBe(circularObject.body)
7
+ expect(circularObject.body.children[1].parent).toBe(circularObject.body)
8
+ expect(circularObject.children[0]).toBe(circularObject.body)
9
+ expect(circularObject.head.parent).toBe(circularObject)
10
+ expect(circularObject.head.children[0].parent).toBe(circularObject.head)
11
+ expect(circularObject.head.children[1].parent).toBe(circularObject.head)
12
+ expect(circularObject.children[1]).toBe(circularObject.head)
13
+ })
14
+ })
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Simple way to count string occurrences for testing.
3
+ * @function
4
+ * @memberOf module:testHelpers
5
+ * @param {string} content
6
+ * @param {string} search
7
+ * @returns {number}
8
+ */
9
+ const countMatches = (content, search) => content.split(search).length - 1
10
+
11
+ module.exports = countMatches
@@ -0,0 +1,9 @@
1
+ const countMatches = require('./countMatches')
2
+
3
+ describe('countMatches', () => {
4
+ test('can get count of string occurrences', () => {
5
+ const find = 'find me'
6
+ const sampleString = `${find} in some string that could change the times ${find} will occur here, ${find} should be found three times`
7
+ expect(countMatches(sampleString, find)).toBe(3)
8
+ })
9
+ })
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Sample object with deep references.
3
+ * @memberOf module:testHelpers
4
+ * @type {Object.<string, string|number|Object>}
5
+ */
6
+ const deepReferenceObject = {
7
+ object1: {
8
+ name: 'someName',
9
+ object2: {
10
+ age: 12,
11
+ array1: [
12
+ 'someString',
13
+ 'anotherString'
14
+ ]
15
+ },
16
+ array2: [
17
+ 89,
18
+ 32
19
+ ]
20
+ },
21
+ title: 'Some Title',
22
+ item: 45
23
+ }
24
+
25
+ module.exports = deepReferenceObject
@@ -0,0 +1,7 @@
1
+ const deepReferenceObject = require('./deepReferenceObject')
2
+
3
+ describe('deepReferenceObject', () => {
4
+ test('has sample deep reference object', () => {
5
+ expect(deepReferenceObject).toHaveProperty('object1.object2.array1')
6
+ })
7
+ })
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Sample of domItem child with nested child and optional details
3
+ * @memberOf module:testHelpers
4
+ * @type {Object.<string, string|number|Array|Object>}
5
+ */
6
+ const domItem = [
7
+ {
8
+ attributes: { className: 'row', style: {} },
9
+ axis: 'y',
10
+ children: [
11
+ {
12
+ attributes: { style: {} },
13
+ axis: 'x',
14
+ children: [],
15
+ element: {},
16
+ eventListeners: {},
17
+ hasShip: false,
18
+ isHit: false,
19
+ parentItem: {},
20
+ point: {},
21
+ tagName: 'div'
22
+ }
23
+ ],
24
+ element: null,
25
+ eventListeners: {},
26
+ parentItem: {},
27
+ tagName: 'div'
28
+ }
29
+ ]
30
+
31
+ module.exports = domItem
@@ -0,0 +1,12 @@
1
+ const domItem = require('./domItem')
2
+
3
+ describe('domItem', () => {
4
+ test('has sample domItem object', () => {
5
+ expect(domItem[0]).toHaveProperty('attributes')
6
+ expect(domItem[0]).toHaveProperty('children')
7
+ expect(domItem[0]).toHaveProperty('element')
8
+ expect(domItem[0]).toHaveProperty('eventListeners')
9
+ expect(domItem[0]).toHaveProperty('parentItem')
10
+ expect(domItem[0]).toHaveProperty('tagName')
11
+ })
12
+ })
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Sample of jsonDom object containing empty nested array and objects
3
+ * @memberOf module:testHelpers
4
+ * @type {Object.<string, string|number|Array|Object>}
5
+ */
6
+ const jsonDom = {
7
+ tagName: 'div',
8
+ attributes: { style: {}, className: 'column' },
9
+ element: null,
10
+ eventListeners: {},
11
+ parentItem: {},
12
+ children: [],
13
+ axis: 'x'
14
+ }
15
+
16
+ module.exports = jsonDom
@@ -0,0 +1,12 @@
1
+ const jsonDom = require('./jsonDom')
2
+
3
+ describe('jsonDom', () => {
4
+ test('has sample jsonDom object', () => {
5
+ expect(jsonDom).toHaveProperty('attributes')
6
+ expect(jsonDom).toHaveProperty('children')
7
+ expect(jsonDom).toHaveProperty('element')
8
+ expect(jsonDom).toHaveProperty('eventListeners')
9
+ expect(jsonDom).toHaveProperty('parentItem')
10
+ expect(jsonDom).toHaveProperty('tagName')
11
+ })
12
+ })
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Sample LinkedList for testing circular references.
3
+ * @memberOf module:testHelpers
4
+ * @type {Object.<string, string|Object>}
5
+ */
6
+ const linkedList = { name: 'one', prev: null, next: null }
7
+ linkedList.next = { name: 'two', prev: linkedList, next: null }
8
+ linkedList.next.next = { name: 'three', prev: linkedList.next, next: null }
9
+
10
+ module.exports = linkedList
@@ -0,0 +1,22 @@
1
+ const linkedList = require('./linkedList')
2
+
3
+ describe('linkedList', () => {
4
+ test('has sample linkedList object', () => {
5
+ expect(linkedList.name).toBe('one')
6
+ expect(linkedList).toHaveProperty('prev')
7
+ expect(linkedList.prev).toBeNull()
8
+
9
+ expect(linkedList).toHaveProperty('next')
10
+ expect(linkedList.next.name).toBe('two')
11
+ expect(linkedList).toHaveProperty('next.prev')
12
+ expect(linkedList.next.prev).toBe(linkedList)
13
+
14
+ expect(linkedList).toHaveProperty('next.next')
15
+ expect(linkedList.next.next.name).toBe('three')
16
+ expect(linkedList).toHaveProperty('next.prev')
17
+ expect(linkedList.next.next.prev).toBe(linkedList.next)
18
+
19
+ expect(linkedList).toHaveProperty('next.next.next')
20
+ expect(linkedList.next.next.next).toBeNull()
21
+ })
22
+ })
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Log out an object in a nicely formatted way.
3
+ * @function
4
+ * @memberOf module:testHelpers
5
+ * @param {Object} object
6
+ * @param {string} [label=logging]
7
+ * @param {string} [outputType=log]
8
+ * @returns {string|undefined}
9
+ */
10
+ const logObject = (object, label = 'logging', outputType = 'log') => {
11
+ const logger = outputType === 'string' ? (label, object) => `'${label}' | ` + JSON.stringify(object) : console[outputType]
12
+ if (typeof require === 'undefined' || outputType === 'string') {
13
+ return logger(label, object)
14
+ }
15
+ return logger(label, require('util').inspect(object, false, null, true))
16
+ }
17
+
18
+ module.exports = logObject
@@ -0,0 +1,20 @@
1
+ const logObject = require('./logObject')
2
+ const util = require('util')
3
+
4
+ describe('logObject', () => {
5
+ test('can nicely output objects', () => {
6
+ const logSpy = jest.spyOn(console, 'log').mockImplementation(jest.fn())
7
+ const inspectSpy = jest.spyOn(util, 'inspect')
8
+ const someObject = { one: 1, two: 2, three: 3 }
9
+ logObject(someObject, 'someObject')
10
+ expect(inspectSpy).toHaveBeenCalledWith(someObject, false, null, true)
11
+ expect(logSpy).toHaveBeenCalledWith('someObject', util.inspect(someObject, false, null, true))
12
+ })
13
+
14
+ test('output to string when that argument is provided', () => {
15
+ const someObject = { one: 1, two: 2, three: 3 }
16
+ const label = 'someObject'
17
+ const result = logObject(someObject, label, 'string')
18
+ expect(result).toEqual(`'${label}' | ` + JSON.stringify(someObject))
19
+ })
20
+ })
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Sample of object containing multiple references.
3
+ * @memberOf module:testHelpers
4
+ * @type {Object.<string, string|number|Object>}
5
+ */
6
+ const multiReferenceObject = {
7
+ object1: {
8
+ name: 'someName'
9
+ },
10
+ object2: {
11
+ age: 12
12
+ },
13
+ array1: [
14
+ 'someString',
15
+ 'anotherString'
16
+ ],
17
+ array2: [
18
+ 89,
19
+ 32
20
+ ],
21
+ title: 'Some Title',
22
+ item: 45
23
+ }
24
+
25
+ module.exports = multiReferenceObject
@@ -0,0 +1,14 @@
1
+ const multiReferenceObject = require('./multiReferenceObject')
2
+
3
+ describe('multiReferenceObject', () => {
4
+ test('has sample multiReferenceObject object', () => {
5
+ expect(multiReferenceObject).toHaveProperty('object1.name')
6
+ expect(multiReferenceObject).toHaveProperty('object2.age')
7
+ expect(multiReferenceObject).toHaveProperty('array1.0')
8
+ expect(multiReferenceObject).toHaveProperty('array1.1')
9
+ expect(multiReferenceObject).toHaveProperty('array2.0')
10
+ expect(multiReferenceObject).toHaveProperty('array2.1')
11
+ expect(multiReferenceObject).toHaveProperty('title')
12
+ expect(multiReferenceObject).toHaveProperty('item')
13
+ })
14
+ })
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Sample NodeTree for testing circular references and arrays.
3
+ * @memberOf module:testHelpers
4
+ * @type {Object.<string, string|Object|Array>}
5
+ */
6
+ const nodeTree = { name: 'one', parent: null, children: [] }
7
+ nodeTree.children[0] = { name: 'child one', parent: nodeTree, children: [] }
8
+ nodeTree.children[1] = { name: 'child two', parent: nodeTree, children: [] }
9
+ nodeTree.children[0].children[0] = { name: 'grandchild one', parent: nodeTree.children[0], children: [] }
10
+
11
+ module.exports = nodeTree