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 c2b475d
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 38 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
62 changes: 62 additions & 0 deletions packages/core/src/api/DeviceSdk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* 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");
});
});

describe("without args", () => {
beforeEach(() => {
sdk = new DeviceSdk();
});

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);
});
});
});
2 changes: 1 addition & 1 deletion packages/core/src/api/DeviceSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { makeContainer, MakeContainerProps } from "@root/src/di";
export class DeviceSdk {
container: Container;
/** @internal */
constructor({ stub, loggers }: MakeContainerProps) {
constructor({ stub, loggers }: Partial<MakeContainerProps> = {}) {
// NOTE: MakeContainerProps might not be the exact type here
// For the init of the project this is sufficient, but we might need to
// update the constructor arguments as we go (we might have more than just the container config)
Expand Down
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";
4 changes: 2 additions & 2 deletions packages/core/src/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export type MakeContainerProps = {
export const makeContainer = ({
stub = false,
loggers = [],
}: Partial<MakeContainerProps> = {}) => {
}: Partial<MakeContainerProps>) => {
const container = new Container();

// Uncomment this line to enable the logger middleware
// container.applyMiddleware(logger);

container.load(
configModuleFactory({ stub }),
loggerModuleFactory({ subscribers: loggers })
loggerModuleFactory({ subscribers: loggers }),
// modules go here
);

Expand Down
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",
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const loggerService = {
debug: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
fatal: jest.fn(),
};

let service: ConfigService;
Expand All @@ -36,7 +37,7 @@ describe("DefaultConfigService", () => {
service = new DefaultConfigService(
localDataSource,
remoteDataSource,
loggerService
loggerService,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class DefaultConfigService implements ConfigService {
constructor(
@inject(configTypes.LocalConfigDataSource) local: LocalConfigDataSource,
@inject(configTypes.RemoteConfigDataSource) remote: RemoteConfigDataSource,
@inject(loggerTypes.LoggerService) logger: LoggerService
@inject(loggerTypes.LoggerService) logger: LoggerService,
) {
this._local = local;
this._remote = remote;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("LoggerService", () => {
const log = LogBuilder.build(
{ type: "test" },
{ key: "value" },
"message"
"message",
);

service.info(log);
Expand All @@ -43,7 +43,7 @@ describe("LoggerService", () => {
const log = LogBuilder.build(
{ type: "test" },
{ key: "value" },
"message"
"message",
);

service.debug(log);
Expand All @@ -59,7 +59,7 @@ describe("LoggerService", () => {
const log = LogBuilder.build(
{ type: "test" },
{ key: "value" },
"message"
"message",
);

service.warn(log);
Expand All @@ -75,12 +75,28 @@ describe("LoggerService", () => {
const log = LogBuilder.buildFromError(
new Error("test"),
{ type: "test" },
{ key: "value" }
{ key: "value" },
);

service.error(log);

expect(log.level).toBe(LogLevel.Error);
});
});

describe("fatal", () => {
it("should have the correct LogLevel", () => {
subscriber.log.mockClear();
service = new DefaultLoggerService([subscriber]);
const log = LogBuilder.build(
{ type: "test" },
{ key: "value" },
"message",
);

service.fatal(log);

expect(log.level).toBe(LogLevel.Fatal);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export class DefaultLoggerService implements LoggerService {
});
}

fatal(log: Log): void {
log.setLevel(LogLevel.Fatal);
this._log(log);
}

info(log: Log): void {
log.setLevel(LogLevel.Info);
this._log(log);
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/internal/logger/service/Log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum LogLevel {
Fatal = -1,
Error = 0,
Warning = 1,
Info = 2,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/internal/logger/service/LogBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class LogBuilder {
static buildFromError(
error: SdkError | Error,
context: LogContext = { type: "error" },
data: LogData = {}
data: LogData = {},
) {
const isSdkError = "_tag" in error;

Expand Down
13 changes: 3 additions & 10 deletions packages/core/src/internal/logger/service/LoggerService.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
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[];

info(log: Log): void;
fatal(log: Log): void;
error(log: Log): void;
warn(log: Log): void;
info(log: Log): void;
debug(log: Log): void;
error(log: Log): void;
}

0 comments on commit c2b475d

Please sign in to comment.