mirror of
https://github.com/comatory/fb2iCal
synced 2025-02-17 04:00:45 +01:00
add specs for crawler service
This commit is contained in:
parent
56be0e9730
commit
d9212f707b
9
mocks/logger.mock.js
Normal file
9
mocks/logger.mock.js
Normal file
@ -0,0 +1,9 @@
|
||||
const EventEmitter = require('events')
|
||||
|
||||
class MockLogger extends EventEmitter {
|
||||
log({ message, level, service }) {
|
||||
this.emit('test:log', { message, level, service })
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MockLogger
|
31
mocks/request.mock.js
Normal file
31
mocks/request.mock.js
Normal file
@ -0,0 +1,31 @@
|
||||
const sinon = jest.requireActual('sinon')
|
||||
|
||||
let currentSpy = null
|
||||
|
||||
const mockRequest = (options) => {
|
||||
try {
|
||||
const spy = currentSpy || sinon.spy()
|
||||
|
||||
console.info(`mock request ${JSON.stringify(options)}`)
|
||||
spy(options)
|
||||
} catch (err) {
|
||||
console.trace(err)
|
||||
}
|
||||
}
|
||||
|
||||
const setRequestSpy = () => {
|
||||
const spy = sinon.spy()
|
||||
|
||||
currentSpy = spy
|
||||
return spy
|
||||
}
|
||||
|
||||
const clearRequestSpy = () => {
|
||||
currentSpy = null
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
mockRequest,
|
||||
setRequestSpy,
|
||||
clearRequestSpy,
|
||||
}
|
4791
package-lock.json
generated
4791
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -11,7 +11,7 @@
|
||||
"start": "node lib/index.js",
|
||||
"start:dev": "NODE_ENV=development PORT=3000 nodemon lib/index.js",
|
||||
"start:dev:inspect": "NODE_ENV=development PORT=3000 nodemon --inspect lib/index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"test": "jest"
|
||||
},
|
||||
"keywords": [
|
||||
"facebook",
|
||||
@ -37,6 +37,10 @@
|
||||
"winston-daily-rotate-file": "^4.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^1.19.3"
|
||||
"chai": "^4.2.0",
|
||||
"chai-sinon": "^2.8.1",
|
||||
"jest": "^26.1.0",
|
||||
"nodemon": "^1.19.3",
|
||||
"sinon": "^9.0.2"
|
||||
}
|
||||
}
|
||||
|
74
test/services/crawler.spec.js
Normal file
74
test/services/crawler.spec.js
Normal file
@ -0,0 +1,74 @@
|
||||
const chai = require('chai')
|
||||
const sinon = require('sinon')
|
||||
const { expect } = chai
|
||||
const chaiSinon = require('chai-sinon')
|
||||
const { mockRequest, setRequestSpy, clearRequestSpy } = require('../../mocks/request.mock')
|
||||
|
||||
const crawl = require('../../lib/services/crawler')
|
||||
const MockLogger = require('../../mocks/logger.mock')
|
||||
|
||||
chai.use(chaiSinon)
|
||||
jest.mock('request', () => mockRequest)
|
||||
|
||||
describe(crawl, () => {
|
||||
let logger
|
||||
let request
|
||||
|
||||
beforeEach(() => {
|
||||
logger = new MockLogger()
|
||||
clearRequestSpy()
|
||||
})
|
||||
|
||||
it('should return promise', () => {
|
||||
const promise = crawl('https://abc.xyz', { logger })
|
||||
|
||||
expect(promise).to.be.instanceOf(Promise)
|
||||
})
|
||||
|
||||
it('should level log message', (callback) => {
|
||||
logger.on('test:log', ({ message, level, service }) => {
|
||||
expect(message).to.equal('Crawl started for url: https://abc.xyz')
|
||||
expect(level).to.equal('info')
|
||||
expect(service).to.equal('parser')
|
||||
callback()
|
||||
})
|
||||
|
||||
crawl('https://abc.xyz', { logger })
|
||||
})
|
||||
|
||||
|
||||
it('should call request', () => {
|
||||
const spy = setRequestSpy()
|
||||
|
||||
crawl('https://abc.xyz', { logger })
|
||||
|
||||
expect(spy).to.have.been.calledOnce
|
||||
})
|
||||
|
||||
|
||||
it('should call request with URL', () => {
|
||||
const spy = setRequestSpy()
|
||||
|
||||
crawl('https://zzz.yyy', { logger })
|
||||
|
||||
expect(spy.args[0][0].url).to.equal('https://zzz.yyy')
|
||||
})
|
||||
|
||||
|
||||
it('should call request with `Accept-Language` header value', () => {
|
||||
const spy = setRequestSpy()
|
||||
|
||||
crawl('https://zzz.yyy', { logger })
|
||||
|
||||
expect(spy.args[0][0].headers).to.have.property('Accept-Language', 'en-US, en')
|
||||
})
|
||||
|
||||
|
||||
it('should call request with `User-Agent` header value', () => {
|
||||
const spy = setRequestSpy()
|
||||
|
||||
crawl('https://zzz.yyy', { logger })
|
||||
|
||||
expect(spy.args[0][0].headers).to.have.property('User-Agent', 'request')
|
||||
})
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user