-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test,action][m]: Improve action() function according to comments in #14
PR - #10
- Loading branch information
1 parent
573529f
commit 0da078d
Showing
3 changed files
with
119 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports.camelToSnakeCaseStr = (str) => | ||
str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`) | ||
module.exports.camelToSnakeCase = (obj) => { | ||
const result = {} | ||
Object.keys(obj).map((key) => { | ||
const snakeCasePropName = module.exports.camelToSnakeCaseStr(key) | ||
result[snakeCasePropName] = obj[key] | ||
}) | ||
console.log(result) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const test = require('ava') | ||
const nock = require('nock') | ||
const urlParser = require('url') | ||
|
||
const { Client } = require('../lib/index') | ||
|
||
const config = { | ||
api: 'http://ckan:5000', | ||
authToken: '5f492394-630d-4759-92f4-000f279dd71b', | ||
organizationId: 'demo-organization', | ||
datasetId: 'my-first-dataset', | ||
} | ||
|
||
const client = new Client( | ||
config.authToken, | ||
config.organizationId, | ||
config.datasetId, | ||
config.api | ||
) | ||
|
||
test('pass correct action name and payload', async (t) => { | ||
const responseMock = { | ||
success: true, | ||
result: { | ||
foo: 'bar', | ||
bar: 'foo', | ||
}, | ||
} | ||
|
||
const payload = { | ||
someDataPropString: 'some value', | ||
someDataPropBoolean: true, | ||
} | ||
|
||
const actionName = 'some_action_name' | ||
|
||
let scope = nock(config.api) | ||
.post(`/api/3/action/${actionName}`, (body) => { | ||
t.deepEqual(body, payload) | ||
return true | ||
}) | ||
.reply(200, (url) => { | ||
return responseMock | ||
}) | ||
|
||
const response = await client.action(actionName, payload) | ||
|
||
t.is(scope.isDone(), true) | ||
t.deepEqual(response, responseMock) | ||
}) | ||
|
||
test('able to force get', async (t) => { | ||
const responseMock = { | ||
success: true, | ||
result: { | ||
foo: 'bar', | ||
bar: 'foo', | ||
}, | ||
} | ||
|
||
const payload = { | ||
someDataPropString: 'some value', | ||
someDataPropBoolean: true, | ||
foo: 'bar', | ||
} | ||
|
||
const actionName = 'some_action_name' | ||
|
||
let scope = nock(config.api) | ||
.get(`/api/3/action/${actionName}`) | ||
.query(true) | ||
.reply(200, (url) => { | ||
const queryObject = urlParser.parse(url, true).query | ||
t.is(client.api, config.api) | ||
t.deepEqual(queryObject, { | ||
some_data_prop_string: 'some value', | ||
some_data_prop_boolean: 'true', | ||
foo: 'bar', | ||
}) | ||
return responseMock | ||
}) | ||
|
||
const response = await client.action(actionName, payload, true) | ||
|
||
t.is(scope.isDone(), true) | ||
t.deepEqual(response, responseMock) | ||
}) |