Add tests addLeadingSlash and MessageTracker

This commit is contained in:
Zoë Hoekstra 2022-10-09 12:41:25 +02:00
parent a665688011
commit fb4cc4f707
No known key found for this signature in database
GPG key ID: F9B7B7D8130F3323
2 changed files with 60 additions and 0 deletions

View file

@ -1,4 +1,5 @@
import { expect } from "chai";
import { FileData } from "../../src/interfaces";
import {
fileChangeEventToMsg,
fileRemovalEventToMsg,
@ -30,4 +31,36 @@ describe("messageGenerators", () => {
expect(requestFilenames).to.exist;
});
});
describe("addLeadingSlash", () => {
it("should add a leading slash if a file in a folder is sent", () => {
const msg = fileRemovalEventToMsg({path: "sub/test.js"});
const result = (msg?.params as FileData).filename;
if(result && result.hasOwnProperty("filename"))
expect(result).to.eq("/sub/test.js")
});
});
describe("addLeadingSlash", () => {
it("should not add a leading slash if a file in the root folder is sent", () => {
const msg = fileRemovalEventToMsg({path: "test.js"});
const result = (msg?.params as FileData).filename;
if(result && result.hasOwnProperty("filename"))
expect(result).to.eq("test.js")
});
});
describe("addLeadingSlash", () => {
it("should return with one leading slash if a file in a folder is sent and file already is prefixed", () => {
const msg = fileRemovalEventToMsg({path: "/sub/test.js"});
const result = (msg?.params as FileData).filename;
if(result && result.hasOwnProperty("filename"))
expect(result).to.eq("/sub/test.js")
});
});
});

View file

@ -1,8 +1,35 @@
import { expect } from "chai";
import { messageTracker } from "../../src/networking/messageTracker";
function* range(start: number, end: number) {
for (let i = start; i <= end; i++) {
yield i;
}
}
describe("messageTracker", () => {
it("should exist", () => {
expect(messageTracker).to.exist;
});
it("should return entered value at index", () => {
messageTracker.push({ jsonrpc: "2.0", id: 0, result: "testvalue0" });
const msg = messageTracker.get(0);
expect(msg?.result).to.eq("testvalue0");
});
it("should throw away values after entering more than it's max amount", () => {
for (const i of range(0, 200)) {
messageTracker.push({ jsonrpc: "2.0", id: i, result: `testvalue${i}` });
}
const msg0 = messageTracker.get(0);
const msg1 = messageTracker.get(1);
const msg200 = messageTracker.get(200);
expect(msg0).to.eq(undefined);
expect(msg1?.result).to.eq("testvalue1");
expect(msg200?.result).to.eq("testvalue200");
})
});