Skip to content

Commit

Permalink
chore(test): add tests to existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman committed Feb 13, 2024
1 parent 14f1b4c commit 9c5c7af
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 26 deletions.
3 changes: 2 additions & 1 deletion packages/core/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const config: JestConfigWithTsJest = {
setupFiles: ["<rootDir>/jest.setup.ts"],
testPathIgnorePatterns: ["<rootDir>/lib/"],
collectCoverageFrom: [
// TODO: remove internal when the rest of the files are setup
"src/**/*.ts",
"!src/**/*.stub.ts",
"!src/index.ts",
"!src/api/index.ts",
],
moduleNameMapper: {
"^@internal/(.*)$": "<rootDir>/src/internal/$1",
Expand Down
47 changes: 47 additions & 0 deletions packages/core/src/api/DeviceSdk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable no-restricted-imports */
import pkg from "../../package.json";
import { DeviceSdk } from "./DeviceSdk";

let sdk: DeviceSdk;
const logger = {
log: jest.fn(),
};
describe("DeviceSdk", () => {
describe("clean", () => {
beforeEach(() => {
sdk = new DeviceSdk({ stub: false, loggers: [logger] });
});

it("should create an instance", () => {
expect(sdk).toBeDefined();
expect(sdk).toBeInstanceOf(DeviceSdk);
});

it("should return a clean `version`", async () => {
expect(await sdk.getVersion()).toBe(pkg.version);
});

it("startScan should ....", () => {
expect(sdk.startScan()).toBeFalsy();
});

it("stopScan should ....", () => {
expect(sdk.stopScan()).toBeFalsy();
});
});

describe("stubbed", () => {
beforeEach(() => {
sdk = new DeviceSdk({ stub: true, loggers: [] });
});

it("should create a stubbed version", () => {
expect(sdk).toBeDefined();
expect(sdk).toBeInstanceOf(DeviceSdk);
});

it("should return a stubbed `version`", async () => {
expect(await sdk.getVersion()).toBe("0.0.0-stub.1");
});
});
});
28 changes: 28 additions & 0 deletions packages/core/src/api/DeviceSdkBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DeviceSdk } from "./DeviceSdk";
import { LedgerDeviceSdkBuilder } from "./DeviceSdkBuilder";

let builder: LedgerDeviceSdkBuilder;
const logger = {
log: jest.fn(),
};

describe("LedgerDeviceSdkBuilder", () => {
beforeEach(() => {
builder = new LedgerDeviceSdkBuilder();
});

it("should build a DeviceSdk instance", () => {
const sdk: DeviceSdk = builder.build();
expect(sdk).toBeInstanceOf(DeviceSdk);
});

it("should set the stub flag", () => {
builder.setStub(true);
expect(builder.stub).toBe(true);
});

it("should add a logger", () => {
builder.addLogger(logger);
expect(builder.loggers).toContain(logger);
});
});
5 changes: 1 addition & 4 deletions packages/core/src/api/DeviceSdkBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import { DeviceSdk } from "./DeviceSdk";
export class LedgerDeviceSdkBuilder {
stub = false;
loggers: LoggerSubscriber[] = [];
constructor() {
console.log("New build");
}

build(): DeviceSdk {
return new DeviceSdk({ stub: this.stub, loggers: this.loggers });
}

setStub(stubbed = true): LedgerDeviceSdkBuilder {
setStub(stubbed: boolean): LedgerDeviceSdkBuilder {
this.stub = stubbed;
return this;
}
Expand Down
33 changes: 22 additions & 11 deletions packages/core/src/api/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,31 @@ const log = jest.spyOn(console, "log").mockImplementation(jest.fn());
let logger: ConsoleLogger;
let logObject: Log;
describe("ConsoleLogger", () => {
beforeEach(() => {
logger = new ConsoleLogger();
logObject = LogBuilder.build({}, {}, "test");
});

afterAll(() => {
warn.mockRestore();
info.mockRestore();
debug.mockRestore();
error.mockRestore();
log.mockRestore();
describe("exports", () => {
it("Log", () => {
const lgg = new Log({
messages: [],
data: {},
context: {},
});
expect(lgg).toBeInstanceOf(Log);
});
});

describe("log", () => {
beforeEach(() => {
logger = new ConsoleLogger();
logObject = LogBuilder.build({}, {}, "test");
});

afterAll(() => {
warn.mockRestore();
info.mockRestore();
debug.mockRestore();
error.mockRestore();
log.mockRestore();
});

it("should log Info level", () => {
logObject.setLevel(LogLevel.Info);
logger.log(logObject);
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/api/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { Log, LogLevel };
export type {
LogContext,
LogData,
LoggerSubscriber,
LogMessages,
} from "@internal/logger/service/Log";

Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@

export { DeviceSdk } from "./DeviceSdk";
export { LedgerDeviceSdkBuilder as DeviceSdkBuilder } from "./DeviceSdkBuilder";
export { ConsoleLogger } from "./Logger";
export type {
LogContext,
LogData,
LoggerSubscriber,
LogLevel,
LogMessages,
} from "./Logger";
export { ConsoleLogger, Log } from "./Logger";
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class StubLocalConfigDataSource implements LocalConfigDataSource {
getConfig(): Either<never, Config> {
return Either.of({
name: "DeviceSDK",
version: "0.0.0-mock.1",
version: "0.0.0-stub.1",
});
}
}
8 changes: 0 additions & 8 deletions packages/core/src/internal/logger/service/LoggerService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import { Log, LoggerSubscriber } from "./Log";
/**
* [IDEAS]
* a data object in looger
* context in logger (object || string ?) context.tag / context.type / context.id / context.os ...
* message (string[]) in logger
*
* EXPOSE TO OUSTIDE => MOVE OUT OF INTERNAL
*/

export interface LoggerService {
subscribers: LoggerSubscriber[];
Expand Down

0 comments on commit 9c5c7af

Please sign in to comment.