Skip to content

Commit

Permalink
Merge pull request #11 from davesag/develop
Browse files Browse the repository at this point in the history
[Release, 1.0.2]
  • Loading branch information
davesag authored Jun 25, 2018
2 parents 54cd808 + 57bf791 commit 81ba736
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "MIT",
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,6 +16,7 @@ module.exports = {
checkUniqueCompoundIndex,
checkUniqueIndex,
dataTypes,
listModels,
makeMockModels,
sequelize
}
13 changes: 8 additions & 5 deletions src/makeMockModels.js → src/mockModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions test/unit/listModels.spec.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
9 changes: 5 additions & 4 deletions test/unit/makeMockModels.spec.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 81ba736

Please sign in to comment.