Merge pull request #27 from Hoekstraa/main

Add tests for addLeadingSlash and messageTracker
This commit is contained in:
Alt 2022-10-09 12:47:01 +02:00 committed by GitHub
commit 861906334c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 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,31 @@ 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");
});
});