Skip to content

Commit

Permalink
Merge pull request #1 from adobe/use-yeoman-test
Browse files Browse the repository at this point in the history
use yeoman-test
  • Loading branch information
moritzraho authored Dec 20, 2019
2 parents c2a1fd5 + 5186a6c commit 4a5ddbf
Show file tree
Hide file tree
Showing 8 changed files with 454 additions and 478 deletions.
1 change: 1 addition & 0 deletions generators/add-action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class AddActions extends Generator {
// options are inputs from CLI or yeoman parent generator
this.option('skip-prompt', { default: false })
this.option('adobe-services', { type: String, default: '' })
this.option('skip-install', { type: String, default: false })

// todo throw meaningful error if add actions in a non existing project, but what defines a project?
}
Expand Down
1 change: 1 addition & 0 deletions generators/add-web-assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class AddWebAssets extends Generator {
this.option('adobe-services', { type: String, default: '' }) // todo use real sdkCodes from console

this.option('project-name', { type: String, default: guessProjectName(this) }) // project name is used in html template
this.option('skip-install', { type: String, default: false })

// todo throw meaningful error if add actions/webassets in a non existing project, but how to know if we are in a project?
}
Expand Down
1 change: 1 addition & 0 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CodeGenerator extends Generator {
this.option('skip-prompt', { default: false })
this.option('adobe-services', { type: String, default: '' })
this.option('project-name', { type: String, default: path.basename(process.cwd()) }) // todo get name from console
this.option('skip-install', { type: String, default: false })

// props are passed to templates
this.props = {}
Expand Down
250 changes: 123 additions & 127 deletions test/generators/add-action/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable jest/expect-expect */
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
Expand All @@ -9,152 +8,149 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
jest.mock('yeoman-generator')
const helpers = require('yeoman-test')

const utils = require('../../../lib/utils')

const TheGenerator = require('../../../generators/add-action')
const theGeneratorPath = require.resolve('../../../generators/add-action')
const Generator = require('yeoman-generator')

// spies
const prompt = jest.spyOn(Generator.prototype, 'prompt')
const composeWith = jest.spyOn(Generator.prototype, 'composeWith')
const installDependencies = jest.spyOn(Generator.prototype, 'installDependencies')
beforeAll(() => {
// mock implementations
composeWith.mockReturnValue(undefined)
installDependencies.mockReturnValue(undefined)
})
beforeEach(() => {
prompt.mockClear()
composeWith.mockClear()
installDependencies.mockClear()
})
afterAll(() => {
composeWith.mockRestore()
installDependencies.mockRestore()
})

jest.mock('../../../lib/utils')

describe('prototype', () => {
test('exports a yeoman generator', () => {
expect(TheGenerator.prototype).toBeInstanceOf(Generator)
expect(require(theGeneratorPath).prototype).toBeInstanceOf(Generator)
})
})

describe('implementation', () => {
describe('constructor', () => {
test('accepts adobe-services option', () => {
const spy = jest.spyOn(TheGenerator.prototype, 'option')
// eslint-disable-next-line no-new
new TheGenerator()
expect(spy).toHaveBeenCalledWith('adobe-services', { type: String, default: '' })
spy.mockRestore()
})
test('accepts skip-prompt option', () => {
const spy = jest.spyOn(TheGenerator.prototype, 'option')
// eslint-disable-next-line no-new
new TheGenerator()
expect(spy).toHaveBeenCalledWith('skip-prompt', { default: false })
spy.mockRestore()
})
describe('run', () => {
test('--skip-prompt --adobe-services="analytics,target,campaign-standard"', async () => {
await helpers.run(theGeneratorPath)
.withOptions({ 'skip-prompt': true, 'adobe-services': 'analytics,target,campaign-standard', 'skip-install': false })
// with skip prompt defaults to generic action
// make sure sub generators have been called
expect(composeWith).toHaveBeenCalledTimes(1)
expect(composeWith).toHaveBeenCalledWith(expect.stringContaining('generic/index.js'), expect.objectContaining({
'skip-prompt': true
}))
expect(installDependencies).toHaveBeenCalledTimes(1)
})

test('--skip-prompt --skip-install', async () => {
await helpers.run(theGeneratorPath)
.withOptions({ 'skip-prompt': true, 'skip-install': true })

// with skip prompt defaults to generic action
// make sure sub generators have been called
expect(composeWith).toHaveBeenCalledTimes(1)
expect(composeWith).toHaveBeenCalledWith(expect.stringContaining('generic/index.js'), expect.objectContaining({
'skip-prompt': true
}))
expect(installDependencies).toHaveBeenCalledTimes(0)
})
describe('prompting with composition', () => {
let prompt
let composeWith
let theGenerator
beforeEach(() => {
prompt = jest.spyOn(TheGenerator.prototype, 'prompt')
composeWith = jest.spyOn(TheGenerator.prototype, 'composeWith')
theGenerator = new TheGenerator()
})
afterEach(() => {
prompt.mockRestore()
composeWith.mockRestore()
})

async function testPromptChoices (adobeServices, expectedChoices) {
// mock selected generator
prompt.mockResolvedValue({
actionGenerators: ['fakeSelection']
})

theGenerator.options = { 'skip-prompt': false, 'adobe-services': adobeServices }
await theGenerator.prompting()
test('--adobe-services="NOTEXISTING" and selects fake generator a', async () => {
await helpers.run(theGeneratorPath)
.withOptions({ 'adobe-services': 'NOTEXITING', 'skip-install': false })
.withPrompts({ actionGenerators: ['a'] })

expect(prompt).toHaveBeenCalledWith([expect.objectContaining({
// first make sure choices are displayed
expect(prompt).toHaveBeenCalledTimes(1)
expect(prompt).toHaveBeenCalledWith([
expect.objectContaining({
type: 'checkbox',
name: 'actionGenerators',
choices: expectedChoices,
validate: utils.atLeastOne,
type: 'checkbox'
})])
}

test('skip-prompt=false, check prompt choices for adobe-services=""', testPromptChoices.bind(this, '', [
{ name: 'Generic', value: expect.stringContaining('generic/index.js'), checked: true }
]))

// todo should we throw instead of ignore ?
test('skip-prompt=false, check prompt choices for adobe-services="notAservice"', testPromptChoices.bind(this, 'notAService', [
{ name: 'Generic', value: expect.stringContaining('generic/index.js'), checked: true }
]))

test('skip-prompt=false, check prompt choices for adobe-services="analytics"', testPromptChoices.bind(this, 'analytics', [
{ name: 'Adobe Analytics', value: expect.stringContaining('analytics/index.js') },
{ name: 'Generic', value: expect.stringContaining('generic/index.js'), checked: true }
]))

test('skip-prompt=false, check prompt choices for adobe-services=" analytics ,target "', testPromptChoices.bind(this, ' analytics ,target ', [
{ name: 'Adobe Analytics', value: expect.stringContaining('analytics/index.js') },
{ name: 'Adobe Target', value: expect.stringContaining('target/index.js') },
{ name: 'Generic', value: expect.stringContaining('generic/index.js'), checked: true }
]))

test('skip-prompt=false, check prompt choices for adobe-services=" analytics ,target , campaign-standard"', testPromptChoices.bind(this, ' analytics ,target , campaign-standard', [
{ name: 'Adobe Analytics', value: expect.stringContaining('analytics/index.js') },
{ name: 'Adobe Target', value: expect.stringContaining('target/index.js') },
{ name: 'Adobe Campaign Standard', value: expect.stringContaining('campaign-standard/index.js') },
{ name: 'Generic', value: expect.stringContaining('generic/index.js'), checked: true }
]))

test('skip-prompt=true"', async () => {
theGenerator.options = { 'skip-prompt': true, 'adobe-services': 'analytics, target, campaign-standard' }
await theGenerator.prompting()

expect(prompt).toHaveBeenCalledTimes(0)

expect(composeWith).toHaveBeenCalledTimes(1)
// if skip prompt makes sure defaults to generic
expect(composeWith).toHaveBeenCalledWith(expect.stringContaining('generic/index.js'), {
'skip-prompt': true
choices: [
{ name: 'Generic', value: expect.stringContaining('generic/index.js'), checked: true }
]
})
})
])

test('skip-prompt=false and user selected ["a", "b", "c"] (should call all three generators)', async () => {
// mock selection of generators
prompt.mockResolvedValue({
actionGenerators: ['a', 'b', 'c']
})
expect(composeWith).toHaveBeenCalledTimes(1)
expect(composeWith).toHaveBeenCalledWith('a', expect.objectContaining({
'skip-prompt': false
}))
expect(installDependencies).toHaveBeenCalledTimes(1)
})

theGenerator.options = { 'skip-prompt': false, 'adobe-services': '' }
await theGenerator.prompting()
test('--adobe-services="analytics" and selects fake generator a', async () => {
await helpers.run(theGeneratorPath)
.withOptions({ 'adobe-services': 'analytics', 'skip-install': false })
.withPrompts({ actionGenerators: ['a'] })

expect(prompt).toHaveBeenCalledTimes(1)
expect(composeWith).toHaveBeenCalledTimes(3)
// if skip prompt makes sure defaults to generic
expect(composeWith).toHaveBeenCalledWith('a', {
'skip-prompt': false
})
expect(composeWith).toHaveBeenCalledWith('b', {
'skip-prompt': false
})
expect(composeWith).toHaveBeenCalledWith('c', {
'skip-prompt': false
// first make sure choices are displayed
expect(prompt).toHaveBeenCalledTimes(1)
expect(prompt).toHaveBeenCalledWith([
expect.objectContaining({
type: 'checkbox',
name: 'actionGenerators',
validate: utils.atLeastOne,
choices: [
{ name: 'Adobe Analytics', value: expect.stringContaining('analytics/index.js') },
{ name: 'Generic', value: expect.stringContaining('generic/index.js'), checked: true }
]
})
})
])

expect(composeWith).toHaveBeenCalledTimes(1)
expect(composeWith).toHaveBeenCalledWith('a', expect.objectContaining({
'skip-prompt': false
}))
expect(installDependencies).toHaveBeenCalledTimes(1)
})

describe('install', () => {
let installDependencies
let theGenerator
beforeEach(() => {
installDependencies = jest.spyOn(TheGenerator.prototype, 'installDependencies')
theGenerator = new TheGenerator()
})
afterEach(() => {
installDependencies.mockRestore()
})

test('skip-install=false', async () => {
theGenerator.options = { 'skip-install': false }
await theGenerator.install()
expect(installDependencies).toBeCalledTimes(1)
})

test('skip-install=true', async () => {
theGenerator.options = { 'skip-install': true }
await theGenerator.install()
expect(installDependencies).toBeCalledTimes(0)
})
test('--adobe-services="analytics,target,campaign-standard" and selects fake generators a,b,c', async () => {
await helpers.run(theGeneratorPath)
.withOptions({ 'adobe-services': 'analytics,target,campaign-standard', 'skip-install': false })
.withPrompts({ actionGenerators: ['a', 'b', 'c'] })

// first make sure choices are displayed
expect(prompt).toHaveBeenCalledTimes(1)
expect(prompt).toHaveBeenCalledWith([
expect.objectContaining({
type: 'checkbox',
name: 'actionGenerators',
validate: utils.atLeastOne,
choices: [
{ name: 'Adobe Analytics', value: expect.stringContaining('analytics/index.js') },
{ name: 'Adobe Target', value: expect.stringContaining('target/index.js') },
{ name: 'Adobe Campaign Standard', value: expect.stringContaining('campaign-standard/index.js') },
{ name: 'Generic', value: expect.stringContaining('generic/index.js'), checked: true }
]
})
])

expect(composeWith).toHaveBeenCalledTimes(3)
expect(composeWith).toHaveBeenCalledWith('a', expect.objectContaining({
'skip-prompt': false
}))
expect(composeWith).toHaveBeenCalledWith('b', expect.objectContaining({
'skip-prompt': false
}))
expect(composeWith).toHaveBeenCalledWith('c', expect.objectContaining({
'skip-prompt': false
}))
expect(installDependencies).toHaveBeenCalledTimes(1)
})
})

// todo check with existing files in project
Loading

0 comments on commit 4a5ddbf

Please sign in to comment.