diff --git a/README.md b/README.md index 611f1df..1d38d7c 100644 --- a/README.md +++ b/README.md @@ -273,6 +273,16 @@ This is where `makeMockModels`, `sinon`, and [`proxyquire`](https://github.com/t As a convenience, `makeMockModels` will automatically populate your `mockModels` with mocks of all of the models defined in your `src/models` folder (or if you have a `.sequelizerc` file it will look for the `model-path` in that). Simply override any of the specific models you need to do stuff with. +### Listing your models + +It's useful to be able to generate a list of the names of your models. + + const { listModels } = require('sequelize-test-helpers') + + console.log(listModels()) // will spit out a list of your model names. + +Similarly to `makeMockModels` above, `listModels` will find all of the models defined in your `src/models` folder (or if you have a `.sequelizerc` file it will look for the `model-path` in that). + ## Contributing Please see the [contributing notes](CONTRIBUTING.md). diff --git a/package.json b/package.json index 7c4b39b..136d007 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sequelize-test-helpers", - "version": "1.0.1", + "version": "1.0.2", "description": "A collection of utilities to help with unit-testing sequelize models", "author": "Dave Sag ", "license": "MIT", diff --git a/src/index.js b/src/index.js index 9913392..26c8cb8 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,7 @@ const checkUniqueCompoundIndex = require('./checkUniqueCompoundIndex') const checkUniqueIndex = require('./checkUniqueIndex') const dataTypes = require('./dataTypes') const sequelize = require('./sequelize') -const makeMockModels = require('./makeMockModels') +const { makeMockModels, listModels } = require('./mockModels') module.exports = { checkHookDefined, @@ -16,6 +16,7 @@ module.exports = { checkUniqueCompoundIndex, checkUniqueIndex, dataTypes, + listModels, makeMockModels, sequelize } diff --git a/src/makeMockModels.js b/src/mockModels.js similarity index 79% rename from src/makeMockModels.js rename to src/mockModels.js index 26fbf20..3fed78a 100644 --- a/src/makeMockModels.js +++ b/src/mockModels.js @@ -26,19 +26,22 @@ const listToObject = (acc, elem) => { return acc } -const finder = folder => +/* istanbul ignore next */ +const listModels = (folder = modelsFolder) => fs .readdirSync(folder) .filter(fileFilter) .map(makeName) - .reduce(listToObject, {}) -/* istanbul ignore next */ +const finder = folder => listModels(folder).reduce(listToObject, {}) -const makeMockModels = (models = {}, folder = modelsFolder) => ({ +const makeMockModels = (models, folder) => ({ ...finder(folder), ...models, '@noCallThru': true }) -module.exports = makeMockModels +module.exports = { + makeMockModels, + listModels +} diff --git a/test/unit/listModels.spec.js b/test/unit/listModels.spec.js new file mode 100644 index 0000000..d43cc9c --- /dev/null +++ b/test/unit/listModels.spec.js @@ -0,0 +1,12 @@ +const { expect } = require('chai') + +const { listModels } = require('../../src') + +describe('src/listModels', () => { + const expected = ['HasHooks', 'Indexed', 'Simple'] + const models = listModels('test/models') + + it('lists the models', () => { + expect(models).to.deep.equal(expected) + }) +}) diff --git a/test/unit/makeMockModels.spec.js b/test/unit/makeMockModels.spec.js index 82216f6..ca51c43 100644 --- a/test/unit/makeMockModels.spec.js +++ b/test/unit/makeMockModels.spec.js @@ -1,16 +1,17 @@ const { expect } = require('chai') -const { makeMockModels } = require('../../src') +const { makeMockModels, listModels } = require('../../src') -const mockModels = makeMockModels({ Fake: 'a fake' }, 'test/models') +describe('src/makeMockModels', () => { + const mockModels = makeMockModels({ Fake: 'a fake' }, 'test/models') + const models = listModels('test/models') -describe('makeMockModels', () => { const doTest = model => { it(`has the model ${model}`, () => { expect(mockModels).to.have.property(model) }) } - ;['Simple', 'HasHooks', 'Indexed', 'Fake'].forEach(doTest) + ;[...models, 'Fake'].forEach(doTest) it("adds '@noCallThru: true'", () => { expect(mockModels).to.have.property('@noCallThru', true)