Skip to content

Commit 74bda1c

Browse files
committed
Add unit tests
1 parent 3dc21db commit 74bda1c

File tree

3 files changed

+265
-0
lines changed

3 files changed

+265
-0
lines changed

src/Wrapper.spec.ts

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
import * as chai from "chai"
2+
const { expect } = chai
3+
import * as sinon from 'sinon';
4+
5+
import * as Nano from "nano"
6+
import { Request } from "request"
7+
import { Wrapper } from "./Wrapper"
8+
9+
describe("Wrapper", () => {
10+
it("wrapServerScope", () => {
11+
const dbName = 'mydb'
12+
const databaseScope = { } as Nano.DatabaseScope
13+
const serverScope = {
14+
use: (_dbName) => { expect(_dbName).to.equal(dbName) },
15+
db: databaseScope
16+
} as Nano.ServerScope
17+
const serverScopeAsync = Wrapper.wrapServerScope(serverScope)
18+
expect(serverScopeAsync).to.have.property("db").that.is.an("Object")
19+
expect(serverScopeAsync).to.have.property("scope").that.is.a("Function")
20+
expect(serverScopeAsync).to.have.property("authAsync").that.is.a("Function")
21+
expect(serverScopeAsync).to.have.property("sessionAsync").that.is.a("Function")
22+
expect(serverScopeAsync).to.have.property("updatesAsync").that.is.a("Function")
23+
expect(serverScopeAsync).to.have.property("uuidsAsync").that.is.a("Function")
24+
})
25+
describe("wrapDatabaseScope", () => {
26+
const headers = { header: "value" }
27+
const createResponse = { ok: true } as Nano.DatabaseCreateResponse
28+
const getResponse = {} as Nano.DatabaseGetResponse
29+
const destroyResponse = {} as Nano.OkResponse
30+
const listResponse = {} as string[]
31+
const compactResponse = {} as Nano.OkResponse
32+
const replicateResponse = {} as Nano.DatabaseReplicateResponse
33+
const changesResponse = {} as Nano.DatabaseChangesResponse
34+
const updatesResponse = {} as Nano.DatabaseUpdatesResponse
35+
36+
function compact(name: string, callback?: Nano.Callback<Nano.OkResponse>): Request
37+
function compact(name: string, designname: string, callback?: Nano.Callback<Nano.OkResponse>): Request
38+
function compact(name: string, arg2?: string | Nano.Callback<Nano.OkResponse>, arg3?: Nano.Callback<Nano.OkResponse>): Request {
39+
const callback = typeof arg2 === "function" ? arg2 : arg3
40+
if (callback) {
41+
callback(null, compactResponse, headers);
42+
}
43+
return {} as Request
44+
}
45+
46+
function replicate<D>(source: string | Nano.DocumentScope <D>, target: string | Nano.DocumentScope <D>,
47+
callback?: Nano.Callback<Nano.DatabaseReplicateResponse>): Request
48+
function replicate<D>(source: string | Nano.DocumentScope <D>, target: string | Nano.DocumentScope <D>,
49+
options: Nano.DatabaseReplicateOptions, callback?: Nano.Callback<Nano.DatabaseReplicateResponse>): Request
50+
function replicate<D>(source: string | Nano.DocumentScope<D>, target: string | Nano.DocumentScope<D>,
51+
arg3?: Nano.DatabaseReplicateOptions | Nano.Callback<Nano.DatabaseReplicateResponse>,
52+
arg4?: Nano.Callback<Nano.DatabaseReplicateResponse>): Request {
53+
const callback = typeof arg3 === "function" ? arg3 : arg4
54+
if (callback) {
55+
callback(null, replicateResponse, headers)
56+
}
57+
return {} as Request
58+
}
59+
60+
function changes(name: string, callback?: Nano.Callback<Nano.DatabaseChangesResponse>): Request
61+
function changes(name: string, params: Nano.DatabaseChangesParams, callback?: Nano.Callback<Nano.DatabaseChangesResponse>): Request
62+
function changes(name: string, arg2?: Nano.DatabaseChangesParams | Nano.Callback<Nano.DatabaseChangesResponse>,
63+
arg3?: Nano.Callback<Nano.DatabaseChangesResponse>): Request {
64+
const callback = typeof arg2 === "function" ? arg2 : arg3
65+
if (callback) {
66+
callback(null, changesResponse, headers)
67+
}
68+
return {} as Request
69+
}
70+
71+
function updates(callback?: Nano.Callback<Nano.DatabaseUpdatesResponse>): Request
72+
function updates(params: Nano.UpdatesParams, callback?: Nano.Callback<Nano.DatabaseUpdatesResponse>): Request
73+
function updates(arg1?: Nano.UpdatesParams | Nano.Callback<Nano.DatabaseUpdatesResponse>,
74+
arg2?: Nano.Callback<Nano.DatabaseUpdatesResponse>): Request {
75+
const callback = typeof arg1 === "function" ? arg1 : arg2
76+
if (callback) {
77+
callback(null, updatesResponse, headers)
78+
}
79+
return {} as Request
80+
}
81+
82+
const databaseScope = {} as Nano.DatabaseScope
83+
databaseScope.use = <D>(name: string): Nano.DocumentScope<D> => {
84+
return {} as Nano.DocumentScope<D>
85+
}
86+
databaseScope.create = (name: string, callback: Nano.Callback<Nano.DatabaseCreateResponse>): Request => {
87+
callback(null, createResponse, headers)
88+
return {} as Request
89+
}
90+
databaseScope.get = (name: string, callback: Nano.Callback<Nano.DatabaseGetResponse>): Request => {
91+
callback(null, getResponse, headers)
92+
return {} as Request
93+
}
94+
95+
databaseScope.destroy = (name: string, callback: Nano.Callback<Nano.OkResponse>): Request => {
96+
callback(null, destroyResponse, headers)
97+
return {} as Request
98+
}
99+
100+
databaseScope.list = (callback: Nano.Callback<string[]>): Request => {
101+
callback(null, listResponse, headers)
102+
return {} as Request
103+
}
104+
105+
databaseScope.compact = compact
106+
databaseScope.replicate = replicate
107+
databaseScope.changes = changes
108+
databaseScope.updates = updates
109+
110+
const databaseScopeAsync = Wrapper.wrapDatabaseScope(databaseScope)
111+
112+
it("the created DatabaseScopeAsync should have all Async suffixed methods", () => {
113+
expect(databaseScopeAsync).to.have.property("createAsync").that.is.a("Function")
114+
expect(databaseScopeAsync).to.have.property("getAsync").that.is.a("Function")
115+
expect(databaseScopeAsync).to.have.property("destroyAsync").that.is.a("Function")
116+
expect(databaseScopeAsync).to.have.property("listAsync").that.is.a("Function")
117+
expect(databaseScopeAsync).to.have.property("compactAsync").that.is.a("Function")
118+
expect(databaseScopeAsync).to.have.property("replicateAsync").that.is.a("Function")
119+
expect(databaseScopeAsync).to.have.property("changesAsync").that.is.a("Function")
120+
expect(databaseScopeAsync).to.have.property("updatesAsync").that.is.a("Function")
121+
})
122+
123+
it("createAsync method should pass all arguments to create method", () => {
124+
const dbName = "mydb"
125+
const databaseScopeCreate = sinon.spy(databaseScope, "create")
126+
return databaseScopeAsync.createAsync(dbName)
127+
.then(([_response, _headers]) => {
128+
expect(databaseScopeCreate.calledOnce)
129+
expect(databaseScopeCreate.calledWith(dbName))
130+
expect(_response).to.equal(createResponse)
131+
expect(_headers).to.equal(headers)
132+
})
133+
})
134+
it("getAsync method should pass all arguments to get method", () => {
135+
const dbName = "mydb"
136+
const get = sinon.spy(databaseScope, "get")
137+
return databaseScopeAsync.getAsync(dbName)
138+
.then(([_response, _headers]) => {
139+
expect(get.calledOnce)
140+
expect(get.calledWith(dbName))
141+
expect(_response).to.equal(getResponse)
142+
expect(_headers).to.equal(headers)
143+
})
144+
})
145+
it("destroyAsync method should pass all arguments to destroy method", () => {
146+
const dbName = "mydb"
147+
const destroy = sinon.spy(databaseScope, "destroy")
148+
return databaseScopeAsync.destroyAsync(dbName)
149+
.then(([_response, _headers]) => {
150+
expect(destroy.calledOnce)
151+
expect(destroy.calledWith(dbName))
152+
expect(_response).to.equal(destroyResponse)
153+
expect(_headers).to.equal(headers)
154+
})
155+
})
156+
it("listAsync method should pass all arguments to list method", () => {
157+
const list = sinon.spy(databaseScope, "list")
158+
return databaseScopeAsync.listAsync()
159+
.then(([_response, _headers]) => {
160+
expect(list.calledOnce)
161+
expect(_response).to.equal(listResponse)
162+
expect(_headers).to.equal(headers)
163+
})
164+
})
165+
it("compactAsync method should pass all arguments to compact method", () => {
166+
const dbName = "mydb"
167+
const compact = sinon.spy(databaseScope, "compact")
168+
return databaseScopeAsync.compactAsync(dbName)
169+
.then(([_response, _headers]) => {
170+
expect(compact.calledOnce)
171+
expect(compact.calledWith(dbName))
172+
expect(_response).to.equal(compactResponse)
173+
expect(_headers).to.equal(headers)
174+
})
175+
})
176+
it("replicateAsync method should pass all arguments to replicate method", () => {
177+
const source = "source"
178+
const target = "target"
179+
const options = { continuous: true }
180+
const replicate = sinon.spy(databaseScope, "replicate")
181+
return databaseScopeAsync.replicateAsync(source, target, options)
182+
.then(([_response, _headers]) => {
183+
expect(replicate.calledOnce)
184+
expect(replicate.calledWith(source, target, options))
185+
expect(_response).to.equal(replicateResponse)
186+
expect(_headers).to.equal(headers)
187+
})
188+
})
189+
it("changesAsync method should pass all arguments to changes method", () => {
190+
const name = "name"
191+
const changes = sinon.spy(databaseScope, "changes")
192+
return databaseScopeAsync.changesAsync(name)
193+
.then(([_response, _headers]) => {
194+
expect(changes.calledOnce)
195+
expect(changes.calledWith(name))
196+
expect(_response).to.equal(changesResponse)
197+
expect(_headers).to.equal(headers)
198+
})
199+
})
200+
it("updatesAsync method should pass all arguments to updates method", () => {
201+
const name = "name"
202+
const updates = sinon.spy(databaseScope, "updates")
203+
return databaseScopeAsync.changesAsync(name)
204+
.then(([_response, _headers]) => {
205+
expect(updates.calledOnce)
206+
expect(updates.calledWith(name))
207+
expect(_response).to.equal(changesResponse)
208+
expect(_headers).to.equal(headers)
209+
})
210+
})
211+
})
212+
})

src/isServerScope.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as chai from "chai"
2+
const { expect } = chai
3+
import nanoFactory = require("nano");
4+
5+
import { isServerScope } from "./isServerScope"
6+
7+
describe("isServerScope", () => {
8+
it("should return true if a ServerScope is passed", () => {
9+
const serverScope = nanoFactory("http://localhost:5984")
10+
const result = isServerScope(serverScope)
11+
expect(result).to.be.true
12+
})
13+
it("should return false if a DocumentScope is passed", () => {
14+
const serverScope = nanoFactory("http://localhost:5984/mydb")
15+
const result = isServerScope(serverScope)
16+
expect(result).to.be.false
17+
})
18+
})

src/promisify.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as chai from "chai"
2+
const { expect } = chai
3+
4+
import { promisify } from "./promisify"
5+
6+
describe("promisify", () => {
7+
const err = new Error("My error")
8+
function myFunc (a: string, b: number, throwError: boolean, callback: (err: Error, param1?: any, param2?: any) => {}) {
9+
if (throwError) {
10+
callback(err)
11+
} else {
12+
callback(null, a, b)
13+
}
14+
}
15+
it("should return function that returns a promise that is resolved if called appropriately", () => {
16+
const promisifiedMyFunc = promisify(myFunc)
17+
return promisifiedMyFunc("alfa", "beta", false)
18+
.then((result: string) => {
19+
expect(result).to.deep.equal(["alfa", "beta"])
20+
return true
21+
})
22+
})
23+
it("should return function that returns a promise that is rejected if called appropriately", (done) => {
24+
const promisifiedMyFunc = promisify(myFunc)
25+
promisifiedMyFunc("alfa", "beta", true)
26+
.then((result: any) => {
27+
done(new Error("This should not happen"))
28+
})
29+
.catch((_err: Error) => {
30+
expect(_err).to.equal(err)
31+
done()
32+
})
33+
})
34+
35+
})

0 commit comments

Comments
 (0)