-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add purify-ts in the template module
- Loading branch information
1 parent
4138604
commit 443b107
Showing
16 changed files
with
372 additions
and
84 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
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
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
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { Either } from "purify-ts"; | ||
import { LocalConfigFailure, RemoteConfigFailure } from "../di/configTypes"; | ||
import { Config } from "../model/Config"; | ||
|
||
// Describe the different data sources interfaces our application could have | ||
|
||
export interface LocalConfigDataSource { | ||
getConfig(): Config; | ||
getConfig(): Either<LocalConfigFailure, Config>; | ||
} | ||
|
||
export interface RemoteConfigDataSource { | ||
getConfig(): Promise<Config>; | ||
getConfig(): Promise<Either<RemoteConfigFailure, Config>>; | ||
} |
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
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
47 changes: 34 additions & 13 deletions
47
packages/core/src/internal/config/data/LocalConfigDataSource.test.ts
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 |
---|---|---|
@@ -1,43 +1,64 @@ | ||
import fs from "fs"; | ||
import { LocalConfigDataSource } from "./ConfigDataSource"; | ||
import { FileLocalConfigDataSource } from "./LocalConfigDataSource"; | ||
import { StubLocalConfigDataSource } from "./LocalConfigDataSource.stub"; | ||
import { Either, Left } from "purify-ts"; | ||
import { JSONParseError, ReadFileError } from "../di/configTypes"; | ||
|
||
const readFileSyncSpy = jest.spyOn(fs, "readFileSync"); | ||
const jsonParse = jest.spyOn(JSON, "parse"); | ||
|
||
let datasource: LocalConfigDataSource; | ||
describe("LocalConfigDataSource", () => { | ||
describe("FileLocalConfigDataSource", () => { | ||
beforeEach(() => { | ||
readFileSyncSpy.mockClear(); | ||
jsonParse.mockClear(); | ||
datasource = new FileLocalConfigDataSource(); | ||
}); | ||
|
||
it("should return an Either<never, Config>", () => { | ||
readFileSyncSpy.mockReturnValue( | ||
JSON.stringify({ name: "DeviceSDK", version: "0.0.0-spied.1" }) | ||
); | ||
}); | ||
|
||
it("should return the config", () => { | ||
expect(datasource.getConfig()).toEqual({ | ||
jsonParse.mockReturnValue({ | ||
name: "DeviceSDK", | ||
version: "0.0.0-spied.1", | ||
}); | ||
|
||
expect(datasource.getConfig()).toStrictEqual( | ||
Either.of({ | ||
name: "DeviceSDK", | ||
version: "0.0.0-spied.1", | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe("StubLocalConfigDataSource", () => { | ||
beforeEach(() => { | ||
datasource = new StubLocalConfigDataSource(); | ||
it("should return an Either<ReadFileError, never> if readFileSync throws", () => { | ||
const err = new Error("readFileSync error"); | ||
readFileSyncSpy.mockImplementation(() => { | ||
throw err; | ||
}); | ||
|
||
expect(datasource.getConfig()).toEqual(Left(new ReadFileError(err))); | ||
}); | ||
|
||
it("should return the config", () => { | ||
expect(datasource.getConfig()).toEqual({ | ||
name: "DeviceSDK", | ||
version: "0.0.0-mock.1", | ||
it("should return an Either<JSONParseError, never> if JSON.parse throws", () => { | ||
const err = new Error("JSON.parse error"); | ||
readFileSyncSpy.mockReturnValue( | ||
JSON.stringify({ name: "DeviceSDK", version: "0.0.0-spied.1" }) | ||
); | ||
|
||
jsonParse.mockImplementation(() => { | ||
throw err; | ||
}); | ||
|
||
expect(datasource.getConfig()).toEqual(Left(new JSONParseError(err))); | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
readFileSyncSpy.mockClear(); | ||
readFileSyncSpy.mockRestore(); | ||
jsonParse.mockRestore(); | ||
}); | ||
}); |
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
13 changes: 8 additions & 5 deletions
13
packages/core/src/internal/config/data/RemoteConfigDataSource.stub.ts
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 |
---|---|---|
@@ -1,19 +1,22 @@ | ||
import { injectable } from "inversify"; | ||
import { RemoteConfigDataSource } from "./ConfigDataSource"; | ||
import { Config } from "../model/Config"; | ||
import { Either } from "purify-ts"; | ||
|
||
/** | ||
* class RemoteRestConfigDataSource | ||
* This is a remote data source that reads the config from a remote API (example). | ||
*/ | ||
@injectable() | ||
export class StubRemoteConfigDataSource implements RemoteConfigDataSource { | ||
async getConfig(): Promise<Config> { | ||
async getConfig(): Promise<Either<never, Config>> { | ||
return new Promise((res) => | ||
res({ | ||
name: "DeviceSDK", | ||
version: "0.0.0-fake.2", | ||
}) | ||
res( | ||
Either.of({ | ||
name: "DeviceSDK", | ||
version: "0.0.0-fake.2", | ||
}) | ||
) | ||
); | ||
} | ||
} |
146 changes: 132 additions & 14 deletions
146
packages/core/src/internal/config/data/RemoteConfigDataSource.test.ts
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
Oops, something went wrong.