Skip to content

Commit

Permalink
Fixed OperatorsLoader.spec
Browse files Browse the repository at this point in the history
  • Loading branch information
elboletaire committed Nov 14, 2018
1 parent 4491ba9 commit ba6a58d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 51 deletions.
20 changes: 4 additions & 16 deletions src/__mocks__/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@ const path = require('path')

const fs = jest.genMockFromModule('fs')

console.log('MOOOOOOOOOOCK LOOOOAAAADEEED')

// This is a custom function that our tests can use during setup to specify
// what the files on the "mock" filesystem should look like when any of the
// `fs` APIs are used.
let mockFiles = Object.create(null)
let dirContents = Object.create(null)
function __setMockFiles(newMockFiles) {
mockFiles = Object.create(null)
for (const file in newMockFiles) {
const dir = path.dirname(file)

if (!mockFiles[dir]) {
mockFiles[dir] = []
}
mockFiles[dir].push(path.basename(file))
}
mockFiles = newMockFiles
}

function __setDirsContents(contents) {
dirContents = contents
function __setDirsContents(mockDirs) {
dirContents = mockDirs
}

// A custom version of `readFileSync` that reads from the special mocked out
Expand All @@ -32,9 +22,7 @@ function readFileSync(filePath) {

Object.keys(mockFiles).forEach((file) => {
const fileRE = new RegExp(file)

if (fileRE.test(filePath)) {
console.log('Returning file contents...', mockFiles[file])
return contents = mockFiles[file]
}
})
Expand All @@ -43,7 +31,7 @@ function readFileSync(filePath) {
}

// A custom version of `readdirSync` that reads from the special mocked out
// file list set via __setDirsContents
// dir list set via __setDirsContents
function readdirSync(directoryPath) {
let contents = []

Expand Down
59 changes: 24 additions & 35 deletions src/services/OperatorsLoader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@ actions:
- name: print-log
type: log
`,
'operator2.yml$': 'file2 contents',
'operator3.yml$': 'file2 contents',
'operator2.yml$': `
# Execute every time a purchase is update
name: eventPurchases
eventName: events.purchase
route: updated
actions:
# Print event purchase logs
- name: print-log
type: log
`,
'operator3.yml$':`
# Execute every time a purchase is update
name: eventPurchases
enabled: false
eventName: events.purchase
route: updated
actions:
# Print event purchase logs
- name: print-log
type: log
`,
};

const MOCK_DIR_INFO = {
Expand All @@ -30,43 +49,13 @@ actions:
require('fs').__setMockFiles(MOCK_FILE_INFO)
require('fs').__setDirsContents(MOCK_DIR_INFO)
});
// beforeEach(() => {
// sinon.stub(fs, 'readdirSync').callsFake(function () {
// return ['operator1.yaml', 'operator2.yml', 'operator3.yml']
// })
// })

// afterEach(() => {
// // remove sinon's stubs
// fs.readdirSync.restore()
// fs.readFileSync.restore()
// })

it('load all the defined operators', () => {
it('load all the defined operators, except those not enabled', () => {
// sinon.stub(fs, 'readFileSync').returns()
const { loadOperators } = require('./OperatorsLoader')
const loadedOperators = loadOperators()
console.log(loadedOperators)

// expect(Array.isArray(loadedOperators)).toBeTruthy()
// expect(loadedOperators.length).toEqual(3)
expect(Array.isArray(loadedOperators)).toBeTruthy()
expect(loadedOperators.length).toEqual(2)
})

// it('should ignore the operator if enabled is set to false', () => {
// sinon.stub(fs, 'readFileSync').returns(`
// # Execute every time a purchase is update
// name: eventPurchases
// enabled: false
// eventName: events.purchase
// route: updated
// actions:
// # Print event purchase logs
// - name: print-log
// type: log
// `)
// const loadedOperators = loadOperators()

// expect(Array.isArray(loadedOperators)).toBeTruthy()
// expect(loadedOperators.length).toEqual(0)
// })
})

0 comments on commit ba6a58d

Please sign in to comment.