-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first tests for MappingsApiProvider (#7)
- Loading branch information
1 parent
a4a88ca
commit 92799ac
Showing
1 changed file
with
96 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const MappingsApiProvider = require("../../providers/mappings-api-provider") | ||
const assert = require("assert") | ||
const MockAdapter = require("axios-mock-adapter") | ||
const errors = require("../../errors") | ||
|
||
const api = { | ||
mappings: "test:/mappings", | ||
concordances: "test:/concordances", | ||
annotations: "test:/annotations", | ||
status: "test:/status", | ||
} | ||
|
||
const registry = new MappingsApiProvider({ | ||
status: api.status, | ||
}) | ||
const mock = new MockAdapter(registry.axios) | ||
|
||
describe("MappingsApiProvider", () => { | ||
|
||
beforeEach(() => { | ||
mock.resetHandlers() | ||
}) | ||
|
||
it("should init the registry correctly via /status", async () => { | ||
mock.onGet(api.status).reply(200, api) | ||
await registry.init() | ||
Object.keys(api).forEach(key => { | ||
assert.equal(registry.api[key], api[key]) | ||
}) | ||
}) | ||
|
||
it("should perform requesting mappings correctly", (done) => { | ||
const requestConfig = { | ||
from: "testFrom", | ||
fromScheme: "testFromScheme", | ||
to: { uri: "testTo" }, | ||
toScheme: { uri: "testToScheme" }, | ||
// TODO: Add more parameters | ||
} | ||
mock.onGet().reply(config => { | ||
assert.equal(config.url, api.mappings) | ||
assert.equal(config.params.from, requestConfig.from) | ||
assert.equal(config.params.fromScheme, requestConfig.fromScheme) | ||
assert.equal(config.params.to, requestConfig.to.uri) | ||
assert.equal(config.params.toScheme, requestConfig.toScheme.uri) | ||
|
||
done() | ||
return [200, []] | ||
}) | ||
registry.getMappings(requestConfig) | ||
}) | ||
|
||
it("should perform requesting a single mapping correctly", (done) => { | ||
const mapping = { | ||
uri: `${api.mappings}/123`, | ||
} | ||
// Simply test whether the correct URL is called | ||
mock.onGet().reply(config => { | ||
assert.equal(config.url, mapping.uri) | ||
|
||
done() | ||
return [200, {}] | ||
}) | ||
registry.getMapping({ mapping }) | ||
}) | ||
|
||
it("should throw an error when calling getMapping without mapping", async () => { | ||
let wasError = false | ||
try { | ||
await registry.getMapping() | ||
} catch(error) { | ||
wasError = true | ||
assert(error instanceof errors.InvalidOrMissingParameterError) | ||
} | ||
assert(wasError) | ||
}) | ||
|
||
it("should throw an error when calling getMapping without mapping URI", async () => { | ||
let wasError = false | ||
try { | ||
await registry.getMapping({ mapping: {} }) | ||
} catch(error) { | ||
wasError = true | ||
assert(error instanceof errors.InvalidOrMissingParameterError) | ||
} | ||
assert(wasError) | ||
}) | ||
|
||
/*** | ||
* TODO: Implement more test. | ||
* | ||
* These tests should assert whether the axios request is performed as expected. | ||
* See also existing tests. | ||
*/ | ||
|
||
}) |