forked from nahanil/node-namesilo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-util.js
executable file
·62 lines (52 loc) · 1.46 KB
/
test-util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* eslint-disable no-undef */
const fs = require('fs')
const path = require('path')
const NameSilo = require('../lib/namesilo')
const fixtures = require('./fixtures/all.js')
/**
* Get sandbox client
* @returns NameSilo
*/
function getClient (options) {
let ns = new NameSilo(Object.assign({}, options, {
apiKey: process.env.NAMESILO_SANDBOX_KEY || 'abc123',
sandbox: true,
// debug: true
}))
ns.axios.post = jest.fn().mockImplementation(ns.axios.post)
ns.post = jest.fn().mockImplementation(ns.post)
return ns
}
function getMockClient (fixture, options) {
options = options || {}
options.apiKey = options.apiKey || '123'
options.sandbox = true
let ns = new NameSilo(options)
let inTestSuite = typeof jest !== 'undefined'
const mockPost = (action, inputs) => {
return new Promise(async (resolve) => {
let data = await loadFixture(fixture || action)
resolve({ data })
})
}
ns.setHTTPClient({
post: inTestSuite ? jest.fn().mockImplementation(mockPost) : mockPost
})
ns.post = inTestSuite ? jest.fn().mockImplementation(ns.post) : ns.post
return ns
}
async function loadFixture (name) {
return new Promise((resolve, reject) => {
fs.readFile(path.join(__dirname, `./fixtures/${name}.xml`), 'utf8', (err, data) => {
if (err) {
return resolve(fixtures[name] ? fixtures[name].sampleResponse : '')
}
resolve(data)
})
})
}
module.exports = {
getClient,
getMockClient,
loadFixture
}