Skip to content

Commit

Permalink
generate selector tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKott committed Jun 4, 2019
1 parent c4d40e9 commit 53f1014
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
10 changes: 4 additions & 6 deletions src/constants/templateStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ export const importAllAsFrom = (as: string, from: string) =>
export const importNamedFrom = (name: string, from: string) =>
`import { ${name} } from '${from}';\n`;

// ACTIONS
export const creatorTestHead = (path: string) =>
`import * as t from '@/actions/types';\n` +
`import * as actions from './creators';\n\n` +
`describe('actions/${path}', () => {\n`;
export const describeTestStart = (path: string) =>
`describe('${path}', () => {\n`;

export const creatorTestFoot = () => '});\n';
export const describeTestEnd = () =>
`});\n`;

// REDUCERS
export const exportDefaultCombineReducers = () =>
Expand Down
13 changes: 8 additions & 5 deletions src/generators/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ export default class ActionGenerator extends Generator implements Zwenerator {
}

addActionCreator() {
const importStatement = t.importAllAsFrom('t', '@/actions/types') + '\n';
const creatorTemplate = this.fs.read(this.templatePath(`${PATH_PREFIX}/creator.ejs`));
const newCreator = ejs.render(creatorTemplate, this.templateConfig);

// read
const creatorsFile = this.fs.read(`${this.absolutePath}/creators.js`, { defaults: importStatement });
const fileHead = t.importAllAsFrom('t', '@/actions/types') + '\n';
const creatorsFile = this.fs.read(`${this.absolutePath}/creators.js`, { defaults: fileHead });
// update
const updateOptions = {
separator: '\n\n',
Expand All @@ -98,13 +98,16 @@ export default class ActionGenerator extends Generator implements Zwenerator {
addActionCreatorTest() {
const testTemplate = this.fs.read(this.templatePath(`${PATH_PREFIX}/creator.test.ejs`));
const newTest = ejs.render(testTemplate, this.templateConfig);
const fileDefaults = t.creatorTestHead(this.destPath);

// read
const testFile = this.fs.read(`${this.absolutePath}/creators.test.js`, { defaults: fileDefaults });
const fileHead =
t.importAllAsFrom('t', '@/actions/types') +
t.importAllAsFrom('actions', './creators') + '\n' +
t.describeTestStart(`actions/${this.destPath}`);
const testFile = this.fs.read(`${this.absolutePath}/creators.test.js`, { defaults: fileHead });
// update
const updateOptions = {
appendixIfNew: t.creatorTestFoot(),
appendixIfNew: t.describeTestEnd(),
replaceStringSeparator: ' ',
separator: '\n\n ',
};
Expand Down
1 change: 0 additions & 1 deletion src/generators/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export default class ComponentGenerator extends Generator implements Zwenerator
}

writing() {
const destPath = `${this.absolutePath}`;
const templateName = this.classComp ? 'classComp' : 'component';
const componentName = this.fileName.toPascalCase();
this.fs.copyTpl(
Expand Down
4 changes: 2 additions & 2 deletions src/generators/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export default class ReducerGenerator extends Generator implements Zwenerator {

destDirWithFileName.forEach((subPath: string) => {
// read
const importWithNewLine = t.importNamedFrom('combineReducers', 'redux') + '\n';
const file = this.fs.read(`${currentPath}/index.js`, { defaults: importWithNewLine });
const fileHead = t.importNamedFrom('combineReducers', 'redux') + '\n';
const file = this.fs.read(`${currentPath}/index.js`, { defaults: fileHead });
// update imports
let updatedFile = addToFile(file, t.importDefaultFrom(subPath, `./${subPath}`), r.selectDefaultImports);

Expand Down
42 changes: 29 additions & 13 deletions src/generators/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const PATH_PREFIX = 'selectors';
const REDUCER_PATH_PREFIX = 'reducers';
const POSSIBLE_STATE_NAMES = 'abcdefghijklmnopqrstuvwxyz'.split('');

const defaultExistingSelector: ExistingSelector =
{ source: 'reducers', path: '', name: '', displayName: '' };
const defaultExistingSelector: ExistingSelector = {
source: 'reducers', path: '', name: '', displayName: '',
};

export default class SelectorGenerator extends Generator implements SelectorZwenerator {
filesToWrite: FileToWrite[] = [];
Expand Down Expand Up @@ -135,7 +136,7 @@ export default class SelectorGenerator extends Generator implements SelectorZwen
configuring() {
this.templateConfig = {
SELECTOR_NAME: this.fileName,
USED_SELECTORS: this.chosenSelectors.map(s => s.name),
USED_SELECTORS: this.chosenSelectors.map(s => s.source === 'reducers' ? `s.${s.name}` : s.name),
STATE_NAMES: POSSIBLE_STATE_NAMES.slice(0, this.chosenSelectors.length).join(', '),
indent: (amount = 1) => this.indent.repeat(amount),
};
Expand All @@ -146,26 +147,26 @@ export default class SelectorGenerator extends Generator implements SelectorZwen

this.destDir.forEach((subPath: string, index: number) => {
// read
const fileDefaults = index === 0 ? t.exportAllFromReducers() : '';
const file = this.fs.read(`${currentPath}/index.js`, { defaults: fileDefaults });
// update imports
const fileHead = index === 0 ? t.exportAllFromReducers() : '';
const file = this.fs.read(`${currentPath}/index.js`, { defaults: fileHead });
// update
const updatedFile = addToFile(file, t.exportAllFrom(subPath), r.selectExportsAll);

// write
this.filesToWrite.push({ name: `${currentPath}/index.js`, content: updatedFile });

currentPath += `/${subPath}`;
});
}

addSelector() {
const importCreateSelector = t.importNamedFrom('createSelector', 'reselect') + '\n';
const selectorTemplate = this.fs.read(this.templatePath(`${PATH_PREFIX}/selector.ejs`));
const newSelector = ejs.render(selectorTemplate, this.templateConfig);

// read
const selectorsFile = this.fs.read(`${this.absolutePath}.js`, { defaults: importCreateSelector });

// TODO: update imports
const fileHead =
t.importNamedFrom('createSelector', 'reselect') + '\n' +
t.importAllAsFrom('s', '@/reducers') + '\n';
const selectorsFile = this.fs.read(`${this.absolutePath}.js`, { defaults: fileHead });
// update
const updateOptions = {
separator: '\n\n',
Expand All @@ -176,8 +177,23 @@ export default class SelectorGenerator extends Generator implements SelectorZwen
}

addSelectorTest() {
// 1. select tests
// 2. add to file (new selector test)
const testTemplate = this.fs.read(this.templatePath(`${PATH_PREFIX}/selector.test.ejs`));
const newTest = ejs.render(testTemplate, this.templateConfig);

// read
const fileHead =
t.importAllAsFrom('s', `./${this.destPath}`) + '\n' +
t.describeTestStart(`selectors/${this.destPath}`);
const testFile = this.fs.read(`${this.absolutePath}.test.js`, { defaults: fileHead });
// update
const updateOptions = {
appendixIfNew: t.describeTestEnd(),
replaceStringSeparator: ' ',
separator: '\n\n ',
};
const updatedFile = addToFile(testFile, newTest, r.selectDescribes, updateOptions);

this.filesToWrite.push({ name: `${this.absolutePath}.test.js`, content: updatedFile });
}

writing() {
Expand Down
8 changes: 8 additions & 0 deletions src/generators/templates/selectors/selector.test.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%=indent()%>describe('<%=SELECTOR_NAME%>', () => {
<%=indent(2)%>it('should return the correct values', () => {
<%=indent(3)%>const state = {};
<%=indent(3)%>const result = s.<%=SELECTOR_NAME%>(state);

<%=indent(3)%>expect(result).toBe('myResult');
<%=indent(2)%>});
<%=indent()%>});

0 comments on commit 53f1014

Please sign in to comment.