forked from confused-Techie/atom-backend
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90d40ce
commit 37def97
Showing
6 changed files
with
159 additions
and
18 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
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,93 @@ | ||
const endpoint = require("../../src/controllers/getThemesSearch.js"); | ||
const database = require("../../src/database.js"); | ||
const context = require("../../src/context.js"); | ||
|
||
describe("Behaves as expected", () => { | ||
test("Calls the correct function", async () => { | ||
const localContext = context; | ||
const spy = jest.spyOn(localContext.database, "simpleSearch"); | ||
|
||
await endpoint.logic({}, localContext); | ||
|
||
expect(spy).toBeCalledTimes(1); | ||
|
||
spy.mockClear(); | ||
}); | ||
|
||
test("Returns zero length array when not found", async () => { | ||
const sso = await endpoint.logic({ | ||
sort: "downloads", | ||
page: "1", | ||
direction: "desc", | ||
query: "hello-world" | ||
}, context); | ||
|
||
expect(sso.ok).toBe(true); | ||
expect(sso.content).toBeArray(); | ||
expect(sso.content.length).toBe(0); | ||
}); | ||
|
||
test("Returns array on success", async () => { | ||
const newPack = await database.insertNewPackage({ | ||
name: "atom-material-syntax", | ||
repository: "https://github.com/confused-Techie/package-backend", | ||
creation_method: "Test Package", | ||
releases: { | ||
latest: "1.1.0" | ||
}, | ||
reamde: "This is a readme!", | ||
metadata: { | ||
name: "atom-material-syntax", | ||
theme: "ui" | ||
}, | ||
versions: { | ||
"1.1.0": { | ||
dist: { | ||
tarball: "download-url", | ||
sha: "1234" | ||
}, | ||
name: "atom-material-syntax" | ||
}, | ||
"1.0.0": { | ||
dist: { | ||
tarball: "download-url", | ||
sha: "1234" | ||
}, | ||
name: "atom-material-syntax" | ||
} | ||
} | ||
}); | ||
|
||
const sso = await endpoint.logic({ | ||
sort: "downloads", | ||
page: "1", | ||
direction: "desc", | ||
query: "atom-material" | ||
}, context); | ||
|
||
expect(sso.ok).toBe(true); | ||
expect(sso.content).toBeArray(); | ||
expect(sso.content.length).toBe(1); | ||
expect(sso.content[0].name).toBe("atom-material-syntax"); | ||
// TODO ensure it matches known good object | ||
await database.removePackageByName("atom-material-syntax", true); | ||
}); | ||
|
||
test("Returns error on db call failure", async () => { | ||
// Moved to last position, since it modifies our shallow copied context | ||
const localContext = context; | ||
localContext.database = { | ||
simpleSearch: () => { return { ok: false, content: "Test Error" } } | ||
}; | ||
|
||
const sso = await endpoint.logic({ | ||
sort: "downloads", | ||
page: "1", | ||
direction: "desc", | ||
query: "hello-world" | ||
}, localContext); | ||
|
||
expect(sso.ok).toBe(false); | ||
expect(sso.content.content).toBe("Test Error"); | ||
}); | ||
}); |
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,54 @@ | ||
const endpoint = require("../../../src/controllers/getThemesSearch.js"); | ||
const context = require("../../../src/context.js"); | ||
|
||
describe("Has features expected", () => { | ||
test("Has correct endpoint features", () => { | ||
const expected = { | ||
method: "GET", | ||
paths: [ "/api/themes/search" ], | ||
rateLimit: "generic", | ||
successStatus: 200 | ||
}; | ||
|
||
expect(endpoint.endpoint).toMatchObject(expected); | ||
}); | ||
|
||
test("Has correct functions", () => { | ||
expect(endpoint.logic).toBeTypeof("function"); | ||
}); | ||
}); | ||
|
||
describe("Parameters behave as expected", () => { | ||
test("Returns valid 'sort'", () => { | ||
const req = { | ||
query: { sort: "downloads" } | ||
}; | ||
|
||
const res = endpoint.params.sort(context, req); | ||
expect(res).toBe("downloads"); | ||
}); | ||
test("Returns valid 'page'", () => { | ||
const req = { | ||
query: { page: "1" } | ||
}; | ||
|
||
const res = endpoint.params.page(context, req); | ||
expect(res).toBe(1); | ||
}); | ||
test("Returns valid 'direction'", () => { | ||
const req = { | ||
query: { direction: "desc" } | ||
}; | ||
|
||
const res = endpoint.params.direction(context, req); | ||
expect(res).toBe("desc"); | ||
}); | ||
test("Returns valid 'query'", () => { | ||
const req = { | ||
query: { q: "hello" } | ||
}; | ||
|
||
const res = endpoint.params.query(context, req); | ||
expect(res).toBe("hello"); | ||
}); | ||
}); |