Skip to content

Commit

Permalink
refactor(types): to refine types for each adapter (#901)
Browse files Browse the repository at this point in the history
* refactor(types): to refine types for each adapter

* refactor: use interfaces for conditionally check adapter args

* fix: use isolatedModules for ts-jest

* fix(github-actions): run actions on pull_request event
  • Loading branch information
emmenko authored and tdeekens committed Jan 5, 2020
1 parent e26393c commit 6c022b8
Show file tree
Hide file tree
Showing 46 changed files with 951 additions and 750 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Test and Build

on: [push]
on: [pull_request]

jobs:
install:
Expand Down
5 changes: 0 additions & 5 deletions .jestrc.lint.json

This file was deleted.

16 changes: 0 additions & 16 deletions .jestrc.test.json

This file was deleted.

5 changes: 5 additions & 0 deletions jest.lint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
runner: 'jest-runner-eslint',
displayName: 'lint',
testMatch: ['<rootDir>/packages/**/*.js', '<rootDir>/packages/**/*.ts'],
};
27 changes: 27 additions & 0 deletions jest.test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
displayName: 'test',
preset: 'ts-jest/presets/js-with-babel',
// Without this option, somehow CI fails to run the tests with the following error:
// TypeError: Unable to require `.d.ts` file.
// This is usually the result of a faulty configuration or import. Make sure there is a `.js`, `.json` or another executable extension available alongside `core.ts`.
// Fix is based on this comment:
// - https://github.com/kulshekhar/ts-jest/issues/805#issuecomment-456055213
// - https://github.com/kulshekhar/ts-jest/blob/master/docs/user/config/isolatedModules.md
globals: {
'ts-jest': {
isolatedModules: true,
},
},
setupFiles: ['raf/polyfill', 'jest-localstorage-mock'],
setupFilesAfterEnv: ['./jest-runner-test.config.js'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'json'],
testEnvironment: 'jsdom',
testURL: 'http://localhost',
watchPlugins: ['jest-plugin-filename', 'jest-watch-yarn-workspaces'],
testPathIgnorePatterns: [
'/node_modules/',
'/packages/.*/node_modules',
'/packages/.*/dist',
],
coveragePathIgnorePatterns: ['/node_modules/'],
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"description": "Monorepository for flipflop and its projects e.g. react-redux, react and the wrapper",
"scripts": {
"postinstall": "check-node-version --package --print",
"develop": "jest --projects .jestrc.*.json --watch",
"lint": "jest --config .jestrc.lint.json --maxWorkers 5",
"develop": "jest --projects jest.*.config.js --watch",
"lint": "jest --config jest.lint.config.js --maxWorkers 5",
"type-check": "tsc --noEmit",
"format": "yarn format:ts && yarn format:md && yarn format:yaml",
"format:ts": "prettier --write '**/*.{js, ts, tsx}'",
"format:md": "prettier --parser markdown --write '**/*.md'",
"format:yaml": "prettier --parser yaml --write '**/*.{yml,yaml}'",
"fix:eslint": "eslint --fix --format=node_modules/eslint-formatter-pretty",
"test": "cross-env NODE_ENV=test jest --config .jestrc.test.json",
"test": "cross-env NODE_ENV=test jest --config jest.test.config.js",
"test:sizes": "bundlesize",
"test:ci": "cross-env NODE_ENV=test yarn test --no-watchman --maxWorkers 5 --no-cache",
"test:ci:coverage": "cross-env NODE_ENV=test yarn test:ci --coverage && codecov",
Expand Down
170 changes: 105 additions & 65 deletions packages/launchdarkly-adapter/modules/adapter/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ describe('when configuring', () => {

describe('with user key', () => {
beforeEach(() => {
return adapter.configure({
clientSideId,
user: userWithKey,
onStatusStateChange,
onFlagsStateChange,
});
return adapter.configure(
{
clientSideId,
user: userWithKey,
},
{
onStatusStateChange,
onFlagsStateChange,
}
);
});

it('should initialize the `ld-client` with `clientSideId` and given `user`', () => {
Expand All @@ -95,12 +99,16 @@ describe('when configuring', () => {

describe('without key', () => {
beforeEach(() =>
adapter.configure({
clientSideId,
user: userWithoutKey,
onStatusStateChange,
onFlagsStateChange,
})
adapter.configure(
{
clientSideId,
user: userWithoutKey,
},
{
onStatusStateChange,
onFlagsStateChange,
}
)
);

it('should initialize the `ld-client` with `clientSideId` and no `user` `key`', () => {
Expand Down Expand Up @@ -138,12 +146,16 @@ describe('when configuring', () => {

ldClient.initialize.mockReturnValue(client);

return adapter.configure({
clientSideId,
user: userWithKey,
onStatusStateChange,
onFlagsStateChange,
});
return adapter.configure(
{
clientSideId,
user: userWithKey,
},
{
onStatusStateChange,
onFlagsStateChange,
}
);
});

describe('when `ldClient` is ready', () => {
Expand Down Expand Up @@ -199,13 +211,17 @@ describe('when configuring', () => {

it('should reject the configuration with an error', async () => {
await expect(
adapter.configure({
clientSideId,
user: userWithKey,
onStatusStateChange,
onFlagsStateChange,
throwOnInitializationFailure: true,
})
adapter.configure(
{
clientSideId,
user: userWithKey,
throwOnInitializationFailure: true,
},
{
onStatusStateChange,
onFlagsStateChange,
}
)
).rejects.toThrow(
'@flopflip/launchdarkly-adapter: adapter failed to initialize.'
);
Expand All @@ -224,13 +240,17 @@ describe('when configuring', () => {

it('should resolve the configuration', async () => {
await expect(
adapter.configure({
clientSideId,
user: userWithKey,
onStatusStateChange,
onFlagsStateChange,
throwOnInitializationFailure: false,
})
adapter.configure(
{
clientSideId,
user: userWithKey,
throwOnInitializationFailure: false,
},
{
onStatusStateChange,
onFlagsStateChange,
}
)
).resolves.toEqual(expect.anything());
});
});
Expand All @@ -248,13 +268,17 @@ describe('when configuring', () => {

ldClient.initialize.mockReturnValue(client);

return adapter.configure({
clientSideId,
user: userWithKey,
onStatusStateChange,
flags: flags,
onFlagsStateChange,
});
return adapter.configure(
{
clientSideId,
user: userWithKey,
flags: flags,
},
{
onStatusStateChange,
onFlagsStateChange,
}
);
});

it('should `dispatch` `onUpdateStatus` action with `isReady`', () => {
Expand Down Expand Up @@ -320,13 +344,17 @@ describe('when configuring', () => {

ldClient.initialize.mockReturnValue(client);

await adapter.configure({
subscribeToFlagChanges: false,
clientSideId,
user: userWithKey,
onStatusStateChange,
onFlagsStateChange,
});
await adapter.configure(
{
subscribeToFlagChanges: false,
clientSideId,
user: userWithKey,
},
{
onStatusStateChange,
onFlagsStateChange,
}
);

onFlagsStateChange.mockClear();

Expand Down Expand Up @@ -357,13 +385,17 @@ describe('when configuring', () => {

ldClient.initialize.mockReturnValue(client);

return adapter.configure({
flagsUpdateDelayMs,
clientSideId,
user: userWithKey,
onStatusStateChange,
onFlagsStateChange,
});
return adapter.configure(
{
flagsUpdateDelayMs,
clientSideId,
user: userWithKey,
},
{
onStatusStateChange,
onFlagsStateChange,
}
);
});

it('should `dispatch` `onFlagsStateChange` action once', () => {
Expand Down Expand Up @@ -406,12 +438,16 @@ describe('when configuring', () => {
ldClient.initialize.mockReturnValue(client);

return adapter
.configure({
clientSideId,
user: userWithKey,
onStatusStateChange,
onFlagsStateChange,
})
.configure(
{
clientSideId,
user: userWithKey,
},
{
onStatusStateChange,
onFlagsStateChange,
}
)
.then(() => adapter.reconfigure({ user: nextUser }));
});

Expand Down Expand Up @@ -440,12 +476,16 @@ describe('when configuring', () => {

ldClient.initialize.mockReturnValue(client);

return adapter.configure({
clientSideId,
user: userWithKey,
onStatusStateChange,
onFlagsStateChange,
});
return adapter.configure(
{
clientSideId,
user: userWithKey,
},
{
onStatusStateChange,
onFlagsStateChange,
}
);
});

describe('with partial prop update', () => {
Expand Down
Loading

0 comments on commit 6c022b8

Please sign in to comment.