Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Add tests for domain logic
Browse files Browse the repository at this point in the history
  • Loading branch information
robinheinze committed Sep 19, 2018
1 parent 1a74ab3 commit 0c15e77
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 13 deletions.
2 changes: 1 addition & 1 deletion commands/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = async function (context) {
return
}

const domainPath = domains.getDomainPath('views', context)
const domainPath = await domains.getDomainPath('views', context)

const name = parameters.first
const pascalName = pascalCase(name)
Expand Down
22 changes: 12 additions & 10 deletions commands/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const domains = require('../lib/domains')

module.exports = async function (context) {
// grab some features
const { parameters, strings, print, ignite, patching } = context
const { parameters, strings, print, ignite, patching, filesystem } = context
const { camelCase, kebabCase, pascalCase, isBlank } = strings

// validation
Expand All @@ -19,26 +19,28 @@ module.exports = async function (context) {
const pascalName = pascalCase(givenName)
const camelName = camelCase(givenName)

const domainPath = domains.getDomainPath('models', context)
const domainPath = await domains.getDomainPath('models', context)

const newDomain = isBlank(domainPath)
const props = { name, pascalName, newDomain }

const props = { name, pascalName }
const jobs = [
{
template: 'model.ejs',
target: `src/models/${domainPath}/${name}.ts`
target: `src/models/${domainPath}${name}/${name}.ts`
}, {
template: 'model.test.ejs',
target: `src/models/${domainPath}/${name}.test.ts`
target: `src/models/${domainPath}${name}/${name}.test.ts`
}
]

const rollupPath = `src/models/${domainPath}/index.ts`
const newDomain = domainPath === name
const rollupPath = `src/models/${domainPath}${name}/index.ts`
const rollupExists = filesystem.exists(rollupPath)

if (newDomain) {
jobs.push({ template: 'rollup-index.ts.ejs', target: rollupPath })
} else {
if (rollupExists) {
patching.insertInFile(rollupPath, 'export', `export * from "./${name}"`, false)
} else {
jobs.push({ template: 'rollup-index.ts.ejs', target: rollupPath })
}

await ignite.copyBatch(context, jobs, props)
Expand Down
2 changes: 1 addition & 1 deletion commands/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = async function (context) {
return
}

const domainPath = domains.getDomainPath('views', context)
const domainPath = await domains.getDomainPath('views', context)

const name = parameters.first
const screenName = name.endsWith('-screen') ? name : `${name}-screen`
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@
"jasmine",
"beforeAll",
"afterAll",
"beforeEach",
"test",
"expect",
"describe"
"describe",
"jest",
"it"
]
},
"devDependencies": {
Expand Down
106 changes: 106 additions & 0 deletions test/domains.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const domains = require('../lib/domains')

describe('for a new domain', () => {
let ask = jest.fn()
let context = {
prompt: {
ask
},
filesystem: {
list: jest.fn()
},
parameters: {
options: {}
}
}
beforeAll(() => {
ask.mockReturnValueOnce(Promise.resolve({ domain: '(Create New)' }))
})

it('returns an empty string', async () => {
const path = await domains.getDomainPath('foo', context)
expect(path).toEqual('')
})
})

describe('for an existing domain', () => {
let ask = jest.fn()
let context = {
prompt: {
ask
},
filesystem: {
list: () => ['users']
},
parameters: {
options: {}
}
}
beforeEach(() => {
ask.mockReturnValueOnce(Promise.resolve({ domain: 'users' }))
})

it('asks the user to choose from available domains', async () => {
await domains.getDomainPath('foo', context)
expect(ask.mock.calls[0][0]).toEqual({
name: 'domain',
type: 'list',
message: 'Add this to which domain?',
choices: ['(Create New)', 'users']
})
})

it('returns the selected path', async () => {
const path = await domains.getDomainPath('foo', context)
expect(path).toEqual('users/')
})
})

describe('when a folder option is passed', () => {
let ask = jest.fn()
let context = {
prompt: {
ask
},
filesystem: {
list: () => ['users']
},
parameters: {
options: {
folder: 'bar'
}
}
}

it('does not prompt the user', async () => {
await domains.getDomainPath('foo', context)
expect(ask.mock.calls.length).toEqual(0)
})

it('returns the specified path', async () => {
const path = await domains.getDomainPath('foo', context)
expect(path).toEqual('bar/')
})
})

describe('when folder option matches the base directory', () => {
let ask = jest.fn()
let context = {
prompt: {
ask
},
filesystem: {
list: () => ['users']
},
parameters: {
options: {
folder: 'models'
}
}
}

it('returns an empty string (same as new domain)', async () => {
const path = await domains.getDomainPath('models', context)
expect(path).toEqual('')
})
})
10 changes: 10 additions & 0 deletions test/generators-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,14 @@ describe('generators', () => {
const lint = await execa('npm', ['-s', 'run', 'lint'])
expect(lint.stderr).toBe('')
})

test('generates a model', async () => {
const simpleModel = 'test'
await execa(IGNITE, ['g', 'model', simpleModel, '--folder', 'models'], { preferLocal: false })
expect(jetpack.exists(`src/models/${simpleModel}/${simpleModel}.ts`)).toBe('file')
expect(jetpack.exists(`src/models/${simpleModel}/${simpleModel}.test.ts`)).toBe('file')
expect(jetpack.exists(`src/models/${simpleModel}/index.ts`)).toBe('file')
const lint = await execa('npm', ['-s', 'run', 'lint'])
expect(lint.stderr).toBe('')
})
})

0 comments on commit 0c15e77

Please sign in to comment.