Skip to content

Commit

Permalink
ci: format files on commit, validate commit msg
Browse files Browse the repository at this point in the history
  • Loading branch information
stas-nc committed Dec 24, 2024
1 parent 3556cff commit 88d9207
Show file tree
Hide file tree
Showing 20 changed files with 1,092 additions and 101 deletions.
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx --no -- commitlint --edit $1
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npm run format:check
npx lint-staged
6 changes: 6 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('@commitlint/types').UserConfig}
*/
module.exports = {
extends: ['@commitlint/config-conventional'],
};
5 changes: 2 additions & 3 deletions ilc/client/BundleLoader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe('BundleLoader', () => {
delete: sinon.stub(),
};
let registry;
let registryConfigurationStub;
const configRoot = getIlcConfigRoot();

const mockFactoryFn = () => {};
Expand Down Expand Up @@ -54,12 +53,12 @@ describe('BundleLoader', () => {
},
},
}).getConfig();
registryConfigurationStub = sinon.stub(configRoot, 'registryConfiguration').value(registry);
sinon.stub(configRoot, 'registryConfiguration').value(registry);
});

afterEach(() => {
SystemJs.import.reset();
registryConfigurationStub.restore();
sinon.stub(configRoot, 'registryConfiguration').restore();
});

describe('preloadApp()', () => {
Expand Down
2 changes: 1 addition & 1 deletion ilc/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class Client {
}

#configure() {
addNavigationHook((url) => (this.#transitionHooksExecutor.hasAccessTo(url) ? url : null));
addNavigationHook((url) => (this.#transitionHooksExecutor.shouldNavigate(url) ? url : null));
addNavigationHook((url) => this.#urlProcessor.process(url));

// TODO: window.ILC.importLibrary - calls bootstrap function with props (if supported), and returns exposed API
Expand Down
10 changes: 5 additions & 5 deletions ilc/client/TransitionHooksExecutor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransitionHookError } from '../common/guard/errors';
import actionTypes from '../common/guard/actionTypes';
import { TransitionHookError } from '../common/transition-hooks/errors';
import { ActionType } from '../common/transition-hooks/ActionType';

/**
* Executes ILC Transition plugin's hooks
Expand All @@ -17,7 +17,7 @@ export default class TransitionHooksExecutor {
this.#logger = logger;
}

hasAccessTo(url) {
shouldNavigate(url) {
const route = this.#router.match(url);
// This code is executed before the router change, so current = previous
const prevRoute = this.#router.getCurrentRoute();
Expand Down Expand Up @@ -47,14 +47,14 @@ export default class TransitionHooksExecutor {
navigate: this.#router.navigateToUrl,
});

if (action.type === actionTypes.stopNavigation) {
if (action.type === ActionType.stopNavigation) {
this.#logger.info(
`ILC: Stopped navigation due to the Route Guard with index #${hooks.indexOf(hook)}`,
);
return false;
}

if (action.type === actionTypes.redirect) {
if (action.type === ActionType.redirect) {
// Need to add redirect callback to queued tasks
// because it should be executed after micro tasks that can be added after the end of this method
setTimeout(() => {
Expand Down
36 changes: 18 additions & 18 deletions ilc/client/TransitionHooksExecutor.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import chai from 'chai';
import sinon from 'sinon';

import { TransitionHookError } from '../common/guard/errors';
import actionTypes from '../common/guard/actionTypes';
import { TransitionHookError } from '../common/transition-hooks/errors';
import { ActionType } from '../common/transition-hooks/ActionType';
import TransitionHooksExecutor from './TransitionHooksExecutor';

describe('TransitionHooksExecutor', () => {
Expand Down Expand Up @@ -64,20 +64,20 @@ describe('TransitionHooksExecutor', () => {

describe('should have access to a provided URL', () => {
it('if router does not have a route that matches a provided URL', () => {
const hooks = [sinon.stub().returns({ type: actionTypes.stopNavigation })];
const hooks = [sinon.stub().returns({ type: ActionType.stopNavigation })];
transitionHooksPlugin.getTransitionHooks.returns(hooks);
router.match.returns({ specialRole: 404 });

const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(transitionHooksExecutor.hasAccessTo('/router/does/not/have/route')).to.be.true;
chai.expect(transitionHooksExecutor.shouldNavigate('/router/does/not/have/route')).to.be.true;
});

it(`if none of hooks returns "${actionTypes.stopNavigation}" or "${actionTypes.redirect}" action types`, () => {
it(`if none of hooks returns "${ActionType.stopNavigation}" or "${ActionType.redirect}" action types`, () => {
const url = '/every/hook/does/not/return/stop/navigation';
const hooks = [
sinon.stub().returns({ type: actionTypes.continue }),
sinon.stub().returns({ type: actionTypes.continue }),
sinon.stub().returns({ type: ActionType.continue }),
sinon.stub().returns({ type: ActionType.continue }),
sinon.stub().returns({ type: null }),
sinon.stub().returns({ type: undefined }),
];
Expand All @@ -87,7 +87,7 @@ describe('TransitionHooksExecutor', () => {

const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.true;
chai.expect(transitionHooksExecutor.shouldNavigate(url)).to.be.true;

for (const hook of hooks) {
sinon.assert.calledOnceWithExactly(hook, {
Expand All @@ -104,7 +104,7 @@ describe('TransitionHooksExecutor', () => {
const error = new Error('Hi there! I am an error. So, should be shown 500 error page in this case');
const url = '/some/hook/returns/stop/navigation';
const hooks = [
sinon.stub().returns({ type: actionTypes.continue }),
sinon.stub().returns({ type: ActionType.continue }),
sinon.stub().throws(error),
sinon.stub().returns({ type: true }),
sinon.stub().returns({ type: 0 }),
Expand All @@ -115,7 +115,7 @@ describe('TransitionHooksExecutor', () => {

const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.false;
chai.expect(transitionHooksExecutor.shouldNavigate(url)).to.be.false;
sinon.assert.calledOnce(errorHandler);
chai.expect(errorHandler.getCall(0).args[0]).to.have.property('cause', error);
chai.expect(errorHandler.getCall(0).args[0]).to.be.instanceOf(TransitionHookError);
Expand All @@ -138,11 +138,11 @@ describe('TransitionHooksExecutor', () => {
}
});

it(`if some of hooks returns "${actionTypes.stopNavigation}" action type`, () => {
it(`if some of hooks returns "${ActionType.stopNavigation}" action type`, () => {
const url = '/some/hook/returns/stop/navigation';
const hooks = [
sinon.stub().returns({ type: actionTypes.continue }),
sinon.stub().returns({ type: actionTypes.stopNavigation }),
sinon.stub().returns({ type: ActionType.continue }),
sinon.stub().returns({ type: ActionType.stopNavigation }),
sinon.stub().returns({ type: true }),
sinon.stub().returns({ type: 0 }),
];
Expand All @@ -152,7 +152,7 @@ describe('TransitionHooksExecutor', () => {

const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.false;
chai.expect(transitionHooksExecutor.shouldNavigate(url)).to.be.false;
sinon.assert.calledOnceWithExactly(
logger.info,
`ILC: Stopped navigation due to the Route Guard with index #${1}`,
Expand All @@ -171,11 +171,11 @@ describe('TransitionHooksExecutor', () => {
}
});

it(`if some of hooks returns "${actionTypes.redirect}" action type`, async () => {
it(`if some of hooks returns "${ActionType.redirect}" action type`, async () => {
const url = '/some/hook/returns/redirect/navigation';
const hooks = [
sinon.stub().returns({ type: actionTypes.continue }),
sinon.stub().returns({ type: actionTypes.redirect, newLocation: url }),
sinon.stub().returns({ type: ActionType.continue }),
sinon.stub().returns({ type: ActionType.redirect, newLocation: url }),
sinon.stub().returns({ type: false }),
sinon.stub().returns({ type: 1 }),
];
Expand All @@ -185,7 +185,7 @@ describe('TransitionHooksExecutor', () => {

const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.false;
chai.expect(transitionHooksExecutor.shouldNavigate(url)).to.be.false;
sinon.assert.notCalled(router.navigateToUrl);

for (const hook of [hooks[0], hooks[1]]) {
Expand Down
10 changes: 1 addition & 9 deletions ilc/common/DefaultCacheWrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,7 @@ describe('DefaultCacheWrapper', () => {
});

it('should return the same value in case of concurrent invocation', async () => {
fn.withArgs().callsFake(
() =>
new Promise((resolve) =>
setTimeout(() => {
console.log('CALLED');
return resolve(data);
}, 100),
),
);
fn.withArgs().callsFake(() => new Promise((resolve) => setTimeout(() => resolve(data), 100)));

const [firstValue, secondValue, thirdValue] = await Promise.all([wrappedFn(), wrappedFn(), wrappedFn()]);

Expand Down
5 changes: 0 additions & 5 deletions ilc/common/guard/actionTypes.js

This file was deleted.

6 changes: 4 additions & 2 deletions ilc/common/router/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import { extendError } from '../utils';

export const RouterError = extendError('RouterError', { defaultData: {} });

export const TEMPLATE_NOT_FOUND_CODE = 1;

export const NoRouteMatchError = extendError('NoRouteMatchError', {
parent: RouterError,
defaultMessage: "Can't find matched route for passed path",
defaultData: {
code: 1, // 1 - TEMPLATE_NOT_FOUND
code: TEMPLATE_NOT_FOUND_CODE,
presentable: 'Template not found',
},
});
export const NoBaseTemplateMatchError = extendError('NoBaseTemplateMatchError', {
parent: RouterError,
defaultMessage: "Can't determine base template for passed route",
defaultData: {
code: 1, // 1 - TEMPLATE_NOT_FOUND
code: TEMPLATE_NOT_FOUND_CODE,
presentable: 'Template not found',
},
});
5 changes: 5 additions & 0 deletions ilc/common/transition-hooks/ActionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum ActionType {
continue = 'continue',
redirect = 'redirect',
stopNavigation = 'stop-navigation',
}
File renamed without changes.
20 changes: 16 additions & 4 deletions ilc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ilc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"fast-glob": "^3.3.2",
"fastify": "^2.15.3",
"http-status-codes": "^2.3.0",
"ilc-plugins-sdk": "^2.2.0",
"ilc-plugins-sdk": "^2.3.0",
"ilc-sdk": "^5.2.4",
"is-url": "^1.2.4",
"js-cookie": "^2.2.1",
Expand Down
Loading

0 comments on commit 88d9207

Please sign in to comment.