Skip to content

Commit

Permalink
♥️ improve plugin flow and tests (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
GantMan authored Mar 31, 2018
1 parent d5ed9d4 commit eb8020d
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 5 deletions.
75 changes: 75 additions & 0 deletions __tests__/command_helpers/__snapshots__/createPlugin.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,81 @@

exports[`check result shape 1`] = `[Function]`;

exports[`checking plugin paths Choice 1 - 'I do not want a generated rule file' 1`] = `
Array [
Array [
".gitignore.ejs",
".gitignore",
],
Array [
"README.md.ejs",
"README.md",
],
Array [
"package.json.ejs",
"package.json",
],
Array [
"simple-plugin.js.ejs",
"extensions/solidarity-nachos.js",
],
]
`;

exports[`checking plugin paths Choice 2 - 'Just a simple rule template' 1`] = `
Array [
Array [
".gitignore.ejs",
".gitignore",
],
Array [
"README.md.ejs",
"README.md",
],
Array [
"package.json.ejs",
"package.json",
],
Array [
"rules-template.json.ejs",
"templates/solidarity-nachos-template.json",
],
Array [
"simple-plugin.js.ejs",
"extensions/solidarity-nachos.js",
],
]
`;

exports[`checking plugin paths Choice 3 - 'Template + optional rules' 1`] = `
Array [
Array [
".gitignore.ejs",
".gitignore",
],
Array [
"README.md.ejs",
"README.md",
],
Array [
"package.json.ejs",
"package.json",
],
Array [
"rules-template.json.ejs",
"templates/solidarity-nachos-template.json",
],
Array [
"helpful-plugin.js.ejs",
"extensions/solidarity-nachos.js",
],
Array [
"addOptionalRules.js.ejs",
"extensions/helpers/addOptionalRules.js",
],
]
`;

exports[`investigate createPlugin 1`] = `
Array [
Array [
Expand Down
40 changes: 40 additions & 0 deletions __tests__/command_helpers/createPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,43 @@ test('investigate createPlugin', async () => {
expect(context.prompt.confirm).toBeCalled()
expect(context.print.success).toBeCalled()
})

describe('checking plugin paths', () => {

beforeEach(() => {
const mockedPrompt = jest
.fn()
.mockImplementationOnce(() => Promise.resolve({ plugin: 'Nachos' }))
.mockImplementationOnce(() => Promise.resolve({ pluginDesc: 'das nachos plugin' }))

context.prompt.ask = mockedPrompt
})

test(`Choice 1 - 'I do not want a generated rule file'`, async () => {
context.prompt.ask.mockImplementationOnce(() => Promise.resolve({
ruleChoice: 'I do not want a generated rule file'
}))

const result = await createPlugin(context)
expect(result).toMatchSnapshot()
})

test(`Choice 2 - 'Just a simple rule template'`, async () => {
context.prompt.ask.mockImplementationOnce(() => Promise.resolve({
ruleChoice: 'Just a simple rule template'
}))

const result = await createPlugin(context)
expect(result).toMatchSnapshot()
})

test(`Choice 3 - 'Template + optional rules'`, async () => {
context.prompt.ask.mockImplementationOnce(() => Promise.resolve({
ruleChoice: 'Template + optional rules'
}))

const result = await createPlugin(context)
expect(result).toMatchSnapshot()
})

})
9 changes: 6 additions & 3 deletions src/extensions/functions/createPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = async context => {
message: 'Plugin name? (we will add the namespacing for you)',
})
if (!answerPluginName.plugin) throw Error('A plugin requires a name')
const pluginName = `solidarity-${answerPluginName.plugin.replace('solidarity-', '')}`
const pluginName = `solidarity-${answerPluginName.plugin.replace('solidarity-', '').toLowerCase()}`

const description = await prompt.ask({
type: 'input',
Expand All @@ -29,7 +29,10 @@ module.exports = async context => {
choices: ruleChoices,
})

if (answer.ruleChoice === ruleChoices[1]) {
const noRuleTemplate = answer.ruleChoice === ruleChoices[0]
if (noRuleTemplate) {
files.push(['simple-plugin.js.ejs', `extensions/${pluginName}.js`])
} else if (answer.ruleChoice === ruleChoices[1]) {
files.push(['rules-template.json.ejs', `templates/${pluginName}-template.json`])
files.push(['simple-plugin.js.ejs', `extensions/${pluginName}.js`])
} else if (answer.ruleChoice === ruleChoices[2]) {
Expand All @@ -46,7 +49,7 @@ module.exports = async context => {
template.generate({
template: fileSet[0],
target: `${pluginName}/${fileSet[1]}`,
props: { pluginName, customRules, description: description.pluginDesc },
props: { pluginName, customRules, description: description.pluginDesc, noRuleTemplate },
})
})

Expand Down
4 changes: 2 additions & 2 deletions src/templates/simple-plugin.js.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module.exports = (context) => {
// Register this plugin
context.addPlugin({
name: '<%= props.pluginName %>',
description: '<%= props.description %>',
snapshot: '<%= props.pluginName %>-template.json'<% if(props.customRules){ %>,
description: '<%= props.description %>'<% if(!props.noRuleTemplate){ %>,
snapshot: '<%= props.pluginName %>-template.json'<% } %><% if(props.customRules){ %>,
rules: {
ruleName: {
check: async (rule, context) => {
Expand Down

0 comments on commit eb8020d

Please sign in to comment.