aboutsummaryrefslogtreecommitdiffstats
path: root/gentests.js
blob: a84c2079d65a1d91cddfea9b1857054c0b179635 (plain) (blame)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const fs = require('fs')
const path = require('path')
const async = require('async')
const promisify = require('pify')

// start(/\.selectors.js/, generateSelectorTest).catch(console.error)
// start(/\.utils.js/, generateUtilTest).catch(console.error)
startContainer(/\.container.js/, generateContainerTest).catch(console.error)

async function getAllFileNames (dirName) {
  const allNames = (await promisify(fs.readdir)(dirName))
  const fileNames = allNames.filter(name => name.match(/^.+\./))
  const dirNames = allNames.filter(name => name.match(/^[^.]+$/))

  const fullPathDirNames = dirNames.map(d => `${dirName}/${d}`)
  const subNameArrays = await promisify(async.map)(fullPathDirNames, getAllFileNames)
  let subNames = []
  subNameArrays.forEach(subNameArray => { subNames = [...subNames, ...subNameArray] })

  return [
    ...fileNames.map(name => dirName + '/' + name),
    ...subNames,
  ]
}

/*
async function start (fileRegEx, testGenerator) {
  const fileNames = await getAllFileNames('./ui/app')
  const sFiles = fileNames.filter(name => name.match(fileRegEx))

  let sFileMethodNames
  let testFilePath
  async.each(sFiles, async (sFile, cb) => {
    const [, sRootPath, sPath] = sFile.match(/^(.+\/)([^/]+)$/)
    sFileMethodNames = Object.keys(require(__dirname + '/' + sFile))

    testFilePath = sPath.replace('.', '-').replace('.', '.test.')

    await promisify(fs.writeFile)(
      `${__dirname}/${sRootPath}tests/${testFilePath}`,
      testGenerator(sPath, sFileMethodNames),
      'utf8'
    )
  }, (err) => {
    console.log(err)
  })

}
*/

async function startContainer (fileRegEx) {
  const fileNames = await getAllFileNames('./ui/app')
  const sFiles = fileNames.filter(name => name.match(fileRegEx))

  async.each(sFiles, async (sFile) => {
    console.log(`sFile`, sFile)
    const [, sRootPath, sPath] = sFile.match(/^(.+\/)([^/]+)$/)

    const testFilePath = sPath.replace('.', '-').replace('.', '.test.')

    await promisify(fs.readFile)(
      path.join(__dirname, sFile),
      'utf8',
      async (err, result) => {
        if (err) {
          console.log('Error: ', err)
        } else {
          console.log(`result`, result.length)
          const returnObjectStrings = result
            .match(/return\s(\{[\s\S]+?})\n}/g)
            .map(str => {
              return str
                .slice(0, str.length - 1)
                .slice(7)
                .replace(/\n/g, '')
                .replace(/\s\s+/g, ' ')

            })
          const mapStateToPropsAssertionObject = returnObjectStrings[0]
            .replace(/\w+:\s\w+\([\w,\s]+\),/g, str => {
              const strKey = str.match(/^\w+/)[0]
              return strKey + ': \'mock' + str.match(/^\w+/)[0].replace(/^./, c => c.toUpperCase()) + ':mockState\',\n'
            })
            .replace(/{\s\w.+/, firstLinePair => `{\n ${firstLinePair.slice(2)}`)
            .replace(/\w+:.+,/g, s => `       ${s}`)
            .replace(/}/g, s => `     ${s}`)
          let mapDispatchToPropsMethodNames
          if (returnObjectStrings[1]) {
            mapDispatchToPropsMethodNames = returnObjectStrings[1].match(/\s\w+:\s/g).map(str => str.match(/\w+/)[0])
          }
          const proxyquireObject = ('{\n  ' + result
            .match(/import\s{[\s\S]+?}\sfrom\s.+/g)
            .map(s => s.replace(/\n/g, ''))
            .map((s) => {
              const proxyKeys = s.match(/{.+}/)[0].match(/\w+/g)
              return '\'' + s.match(/'(.+)'/)[1] + '\': { ' + (proxyKeys.length > 1
                  ? '\n    ' + proxyKeys.join(': () => {},\n    ') + ': () => {},\n '
                  : proxyKeys[0] + ': () => {},') + ' }'
            })
            .join(',\n  ') + '\n}')
            .replace('{ connect: () => {}, },', `{
      connect: (ms, md) => {
        mapStateToProps = ms
        mapDispatchToProps = md
        return () => ({})
      },
    },`)
            // console.log(`proxyquireObject`, proxyquireObject);
          // console.log(`mapStateToPropsAssertionObject`, mapStateToPropsAssertionObject);
          // console.log(`mapDispatchToPropsMethodNames`, mapDispatchToPropsMethodNames);

          const containerTest = generateContainerTest(sPath, {
            mapStateToPropsAssertionObject,
            mapDispatchToPropsMethodNames,
            proxyquireObject,
          })
          // console.log(`containerTest`, `${__dirname}/${sRootPath}tests/${testFilePath}`, containerTest);
          console.log('----')
          console.log(`sRootPath`, sRootPath)
          console.log(`testFilePath`, testFilePath)
          await promisify(fs.writeFile)(
            `${__dirname}/${sRootPath}tests/${testFilePath}`,
            containerTest,
            'utf8'
          )
        }
      }
    )
  }, (err) => {
    console.log('123', err)
  })

}
/*
function generateMethodList (methodArray) {
  return methodArray.map(n => '  ' + n).join(',\n') + ','
}

function generateMethodDescribeBlock (methodName, index) {
  const describeBlock =
  `${index ? '  ' : ''}describe('${methodName}()', () => {
    it('should', () => {
      const state = {}

      assert.equal(${methodName}(state), )
    })
  })`
  return describeBlock
}
*/
function generateDispatchMethodDescribeBlock (methodName, index) {
  const describeBlock =
  `${index ? '    ' : ''}describe('${methodName}()', () => {
      it('should dispatch an action', () => {
        mapDispatchToPropsObject.${methodName}()
        assert(dispatchSpy.calledOnce)
      })
    })`
  return describeBlock
}
/*
function generateMethodDescribeBlocks (methodArray) {
  return methodArray
    .map((methodName, index) => generateMethodDescribeBlock(methodName, index))
    .join('\n\n')
}
*/

function generateDispatchMethodDescribeBlocks (methodArray) {
  return methodArray
    .map((methodName, index) => generateDispatchMethodDescribeBlock(methodName, index))
    .join('\n\n')
}

/*
function generateSelectorTest (name, methodArray) {
return `import assert from 'assert'
import {
${generateMethodList(methodArray)}
} from '../${name}'

describe('${name.match(/^[^.]+/)} selectors', () => {

  ${generateMethodDescribeBlocks(methodArray)}

})`
}

function generateUtilTest (name, methodArray) {
return `import assert from 'assert'
import {
${generateMethodList(methodArray)}
} from '../${name}'

describe('${name.match(/^[^.]+/)} utils', () => {

  ${generateMethodDescribeBlocks(methodArray)}

})`
}
*/

function generateContainerTest (sPath, {
  mapStateToPropsAssertionObject,
  mapDispatchToPropsMethodNames,
  proxyquireObject,
}) {
return `import assert from 'assert'
import proxyquire from 'proxyquire'
import sinon from 'sinon'

let mapStateToProps
let mapDispatchToProps

proxyquire('../${sPath}', ${proxyquireObject})

describe('${sPath.match(/^[^.]+/)} container', () => {

  describe('mapStateToProps()', () => {

    it('should map the correct properties to props', () => {
      assert.deepEqual(mapStateToProps('mockState'), ${mapStateToPropsAssertionObject})
    })
    
  })

  describe('mapDispatchToProps()', () => {
    let dispatchSpy
    let mapDispatchToPropsObject

    beforeEach(() => {
      dispatchSpy = sinon.spy()
      mapDispatchToPropsObject = mapDispatchToProps(dispatchSpy)
    })

    ${mapDispatchToPropsMethodNames ? generateDispatchMethodDescribeBlocks(mapDispatchToPropsMethodNames) : 'delete'}

  })

})`
}