-
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.
test: add unit test for new storages
- Loading branch information
1 parent
1796db2
commit 3b0af1d
Showing
3 changed files
with
430 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,143 @@ | ||
import { Query, StorageService } from "../../storage/storage.types"; | ||
import { | ||
ConnectionNoteRecord, | ||
ConnectionNoteRecordStorageProps, | ||
} from "./connectionNoteRecord"; | ||
import { ConnectionNoteStorage } from "./connectionNoteStorage"; | ||
|
||
const storageService = jest.mocked<StorageService<ConnectionNoteRecord>>({ | ||
save: jest.fn(), | ||
delete: jest.fn(), | ||
deleteById: jest.fn(), | ||
update: jest.fn(), | ||
findById: jest.fn(), | ||
findAllByQuery: jest.fn(), | ||
getAll: jest.fn(), | ||
}); | ||
|
||
const connectionNoteStorage = new ConnectionNoteStorage(storageService); | ||
|
||
const id1 = "id1"; | ||
const id2 = "id2"; | ||
|
||
const now = new Date(); | ||
|
||
const connectionNoteRecordProps: ConnectionNoteRecordStorageProps = { | ||
id: id1, | ||
createdAt: now, | ||
connectionId: "connectionId", | ||
title: "title", | ||
message: "message", | ||
tags: {}, | ||
}; | ||
|
||
const connectionNoteRecordA = new ConnectionNoteRecord( | ||
connectionNoteRecordProps | ||
); | ||
|
||
const connectionNoteRecordB = new ConnectionNoteRecord({ | ||
...connectionNoteRecordProps, | ||
id: id2, | ||
}); | ||
|
||
describe("ConnectionNote Storage", () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test("Should save connectionNote record", async () => { | ||
storageService.save.mockResolvedValue(connectionNoteRecordA); | ||
await connectionNoteStorage.save(connectionNoteRecordProps); | ||
expect(storageService.save).toBeCalledWith(connectionNoteRecordA); | ||
}); | ||
|
||
test("Should delete connectionNote record", async () => { | ||
storageService.delete.mockResolvedValue(); | ||
await connectionNoteStorage.delete(connectionNoteRecordA); | ||
expect(storageService.delete).toBeCalledWith(connectionNoteRecordA); | ||
}); | ||
|
||
test("Should delete connectionNote record by ID", async () => { | ||
storageService.deleteById.mockResolvedValue(); | ||
await connectionNoteStorage.deleteById(connectionNoteRecordA.id); | ||
expect(storageService.deleteById).toBeCalledWith(connectionNoteRecordA.id); | ||
}); | ||
|
||
test("Should update connectionNote record", async () => { | ||
storageService.update.mockResolvedValue(); | ||
await connectionNoteStorage.update(connectionNoteRecordA); | ||
expect(storageService.update).toBeCalledWith(connectionNoteRecordA); | ||
}); | ||
|
||
test("Should find connectionNote record by ID", async () => { | ||
storageService.findById.mockResolvedValue(connectionNoteRecordA); | ||
const result = await connectionNoteStorage.findById( | ||
connectionNoteRecordA.id | ||
); | ||
expect(result).toEqual(connectionNoteRecordA); | ||
}); | ||
|
||
test("Should find all connectionNote records by query", async () => { | ||
const query: Query<ConnectionNoteRecord> = { | ||
connectionId: "connectionId", | ||
}; | ||
const records = [connectionNoteRecordA, connectionNoteRecordB]; | ||
storageService.findAllByQuery.mockResolvedValue(records); | ||
const result = await connectionNoteStorage.findAllByQuery(query); | ||
expect(result).toEqual(records); | ||
}); | ||
|
||
test("Should get all connectionNote records", async () => { | ||
const records = [connectionNoteRecordA, connectionNoteRecordB]; | ||
storageService.getAll.mockResolvedValue(records); | ||
const result = await connectionNoteStorage.getAll(); | ||
expect(result).toEqual(records); | ||
}); | ||
|
||
// tests error | ||
test("Should handle saving error", async () => { | ||
storageService.save.mockRejectedValue(new Error("Saving error")); | ||
await expect( | ||
connectionNoteStorage.save(connectionNoteRecordProps) | ||
).rejects.toThrow("Saving error"); | ||
}); | ||
|
||
test("Should handle deleting error", async () => { | ||
storageService.delete.mockRejectedValue(new Error("Deleting error")); | ||
await expect( | ||
connectionNoteStorage.delete(connectionNoteRecordA) | ||
).rejects.toThrow("Deleting error"); | ||
}); | ||
|
||
test("Should handle updating error", async () => { | ||
storageService.update.mockRejectedValue(new Error("Updating error")); | ||
await expect( | ||
connectionNoteStorage.update(connectionNoteRecordA) | ||
).rejects.toThrow("Updating error"); | ||
}); | ||
|
||
test("Should handle finding error", async () => { | ||
storageService.findById.mockRejectedValue(new Error("Finding error")); | ||
await expect( | ||
connectionNoteStorage.findById(connectionNoteRecordA.id) | ||
).rejects.toThrow("Finding error"); | ||
}); | ||
|
||
test("Should handle not found", async () => { | ||
storageService.findById.mockResolvedValue(null); | ||
const result = await connectionNoteStorage.findById("nonexistentId"); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
test("Should handle empty result", async () => { | ||
storageService.findAllByQuery.mockResolvedValue([]); | ||
const result = await connectionNoteStorage.findAllByQuery({ filter: {} }); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
test("Should handle empty result for getAll", async () => { | ||
storageService.getAll.mockResolvedValue([]); | ||
const result = await connectionNoteStorage.getAll(); | ||
expect(result).toEqual([]); | ||
}); | ||
}); |
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,138 @@ | ||
import { Query, StorageService } from "../../storage/storage.types"; | ||
import { | ||
ConnectionRecord, | ||
ConnectionRecordStorageProps, | ||
} from "./connectionRecord"; | ||
import { ConnectionStorage } from "./connectionStorage"; | ||
|
||
const storageService = jest.mocked<StorageService<ConnectionRecord>>({ | ||
save: jest.fn(), | ||
delete: jest.fn(), | ||
deleteById: jest.fn(), | ||
update: jest.fn(), | ||
findById: jest.fn(), | ||
findAllByQuery: jest.fn(), | ||
getAll: jest.fn(), | ||
}); | ||
|
||
const connectionStorage = new ConnectionStorage(storageService); | ||
|
||
const id1 = "id1"; | ||
const id2 = "id2"; | ||
|
||
const now = new Date(); | ||
|
||
const connectionRecordProps: ConnectionRecordStorageProps = { | ||
id: id1, | ||
createdAt: now, | ||
alias: "alias", | ||
oobi: "oobi", | ||
tags: {}, | ||
}; | ||
|
||
const connectionRecordA = new ConnectionRecord(connectionRecordProps); | ||
|
||
const connectionRecordB = new ConnectionRecord({ | ||
...connectionRecordProps, | ||
id: id2, | ||
}); | ||
|
||
describe("Connection Storage", () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test("Should save connection record", async () => { | ||
storageService.save.mockResolvedValue(connectionRecordA); | ||
await connectionStorage.save(connectionRecordProps); | ||
expect(storageService.save).toBeCalledWith(connectionRecordA); | ||
}); | ||
|
||
test("Should delete connection record", async () => { | ||
storageService.delete.mockResolvedValue(); | ||
await connectionStorage.delete(connectionRecordA); | ||
expect(storageService.delete).toBeCalledWith(connectionRecordA); | ||
}); | ||
|
||
test("Should delete connection record by ID", async () => { | ||
storageService.deleteById.mockResolvedValue(); | ||
await connectionStorage.deleteById(connectionRecordA.id); | ||
expect(storageService.deleteById).toBeCalledWith(connectionRecordA.id); | ||
}); | ||
|
||
test("Should update connection record", async () => { | ||
storageService.update.mockResolvedValue(); | ||
await connectionStorage.update(connectionRecordA); | ||
expect(storageService.update).toBeCalledWith(connectionRecordA); | ||
}); | ||
|
||
test("Should find connection record by ID", async () => { | ||
storageService.findById.mockResolvedValue(connectionRecordA); | ||
const result = await connectionStorage.findById(connectionRecordA.id); | ||
expect(result).toEqual(connectionRecordA); | ||
}); | ||
|
||
test("Should find all connection records by query", async () => { | ||
const query: Query<ConnectionRecord> = { | ||
alias: "alias", | ||
}; | ||
const records = [connectionRecordA, connectionRecordB]; | ||
storageService.findAllByQuery.mockResolvedValue(records); | ||
const result = await connectionStorage.findAllByQuery(query); | ||
expect(result).toEqual(records); | ||
}); | ||
|
||
test("Should get all connection records", async () => { | ||
const records = [connectionRecordA, connectionRecordB]; | ||
storageService.getAll.mockResolvedValue(records); | ||
const result = await connectionStorage.getAll(); | ||
expect(result).toEqual(records); | ||
}); | ||
|
||
// tests error | ||
test("Should handle saving error", async () => { | ||
storageService.save.mockRejectedValue(new Error("Saving error")); | ||
await expect(connectionStorage.save(connectionRecordProps)).rejects.toThrow( | ||
"Saving error" | ||
); | ||
}); | ||
|
||
test("Should handle deleting error", async () => { | ||
storageService.delete.mockRejectedValue(new Error("Deleting error")); | ||
await expect(connectionStorage.delete(connectionRecordA)).rejects.toThrow( | ||
"Deleting error" | ||
); | ||
}); | ||
|
||
test("Should handle updating error", async () => { | ||
storageService.update.mockRejectedValue(new Error("Updating error")); | ||
await expect(connectionStorage.update(connectionRecordA)).rejects.toThrow( | ||
"Updating error" | ||
); | ||
}); | ||
|
||
test("Should handle finding error", async () => { | ||
storageService.findById.mockRejectedValue(new Error("Finding error")); | ||
await expect( | ||
connectionStorage.findById(connectionRecordA.id) | ||
).rejects.toThrow("Finding error"); | ||
}); | ||
|
||
test("Should handle not found", async () => { | ||
storageService.findById.mockResolvedValue(null); | ||
const result = await connectionStorage.findById("nonexistentId"); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
test("Should handle empty result", async () => { | ||
storageService.findAllByQuery.mockResolvedValue([]); | ||
const result = await connectionStorage.findAllByQuery({ filter: {} }); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
test("Should handle empty result for getAll", async () => { | ||
storageService.getAll.mockResolvedValue([]); | ||
const result = await connectionStorage.getAll(); | ||
expect(result).toEqual([]); | ||
}); | ||
}); |
Oops, something went wrong.