Skip to content

Commit

Permalink
Simplify status check logic
Browse files Browse the repository at this point in the history
commit_hash:5efefd792286557b4378bd5f1a8b6aa37c5d8231
  • Loading branch information
sabio committed Oct 25, 2024
1 parent bf50962 commit f4f2678
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 55 deletions.
50 changes: 25 additions & 25 deletions src/providers/statusCheck/__tests__/statusCheck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
CHECK_URL_PARAM,
LANG_URL_PARAM,
DEFAULT_LANGUAGE,
langForCheck,
counterIdForCheck,
getStatusCheckSearchParams,
} from '../urlSearchParams';
import { checkStatusRaw } from '../statusCheck';
import { checkStatusFn } from '../statusCheckFn';
Expand All @@ -22,45 +21,46 @@ describe('CHECK_STATUS_FEATURE', () => {
const locationSearchWithoutLangParameter = `?${CHECK_URL_PARAM}=${counterId}`;
const locationSearchWithoutCheckParameter = `?counterid=${counterId}`;

const windowWithSearchParams = (inputSearchParams: string) => ({
location: {
search: inputSearchParams,
},
});
const windowWithSearchParams = (inputSearchParams: string) =>
({
location: {
search: inputSearchParams,
},
} as Window);

describe('langForCheck', () => {
it(`returns the value of "${LANG_URL_PARAM}" search parameter as defined in location`, () => {
const ctx: any = windowWithSearchParams(
const ctx = windowWithSearchParams(
locationSearchWithCheckParameter,
);
const parsedLang = langForCheck(ctx);
chai.expect(parsedLang).to.equal(lang);
const params = getStatusCheckSearchParams(ctx);
chai.expect(params.lang).to.equal(lang);
});

it(`returns "${DEFAULT_LANGUAGE}" when "${LANG_URL_PARAM}" search parameter is not defined in location`, () => {
const ctx: any = windowWithSearchParams(
const ctx = windowWithSearchParams(
locationSearchWithoutLangParameter,
);
const parsedLang = langForCheck(ctx);
chai.expect(parsedLang).to.equal(DEFAULT_LANGUAGE);
const params = getStatusCheckSearchParams(ctx);
chai.expect(params.lang).to.equal(DEFAULT_LANGUAGE);
});
});

describe('counterIdForCheck', () => {
it(`returns the numeric value of "${CHECK_URL_PARAM}" search parameter as defined in location`, () => {
const ctx: any = windowWithSearchParams(
const ctx = windowWithSearchParams(
locationSearchWithCheckParameter,
);
const parsedCounterId = counterIdForCheck(ctx);
chai.expect(parsedCounterId).to.equal(numericCounterId);
const { id } = getStatusCheckSearchParams(ctx);
chai.expect(id).to.equal(numericCounterId);
});

it(`returns NaN when "${CHECK_URL_PARAM}" search parameter is not defined in location`, () => {
const ctx: any = windowWithSearchParams(
const ctx = windowWithSearchParams(
locationSearchWithoutCheckParameter,
);
const parsedCounterId = counterIdForCheck(ctx);
chai.expect(parsedCounterId).to.be.NaN;
const { id } = getStatusCheckSearchParams(ctx);
chai.expect(id).to.be.NaN;
});
});

Expand Down Expand Up @@ -91,15 +91,15 @@ describe('CHECK_STATUS_FEATURE', () => {
});

it(`does not trigger setupAndLoadScript when the check is disabled in search parameters`, () => {
const ctx: any = windowWithSearchParams(
const ctx = windowWithSearchParams(
locationSearchWithoutCheckParameter,
);
checkStatusRaw(ctx, counterOptions);
sinon.assert.notCalled(setDefer);
});

it(`triggers setupAndLoadScript with counter ID from search parameters`, () => {
const ctx: any = windowWithSearchParams(
const ctx = windowWithSearchParams(
locationSearchWithCheckParameter,
);
checkStatusRaw(ctx, counterOptions);
Expand All @@ -121,7 +121,7 @@ describe('CHECK_STATUS_FEATURE', () => {

describe('checkStatusFn', () => {
it('returns the counter id and checkStatus=true for existing counter', () => {
const ctx: any = {
const ctx = {
Ya: {
_metrika: {
getCounters: () => [{ id: numericCounterId }],
Expand All @@ -130,15 +130,15 @@ describe('CHECK_STATUS_FEATURE', () => {
location: {
search: locationSearchWithCheckParameter,
},
};
} as unknown as Window;

const status = checkStatusFn(ctx);
chai.expect(status.id).to.equal(numericCounterId);
chai.expect(status.counterFound).to.be.true;
});

it('returns the counter id and checkStatus=false for missing counter', () => {
const ctx: any = {
const ctx = {
Ya: {
_metrika: {
getCounters: () => [{ id: 2 }],
Expand All @@ -147,7 +147,7 @@ describe('CHECK_STATUS_FEATURE', () => {
location: {
search: locationSearchWithCheckParameter,
},
};
} as unknown as Window;

const status = checkStatusFn(ctx);
chai.expect(status.id).to.equal(numericCounterId);
Expand Down
9 changes: 3 additions & 6 deletions src/providers/statusCheck/statusCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import {
getResourceUrl,
setupUtilsAndLoadScript,
} from 'src/providers/remoteControl/remoteControl';
import {
counterIdForCheck,
langForCheck,
} from 'src/providers/statusCheck/urlSearchParams';
import { getStatusCheckSearchParams } from 'src/providers/statusCheck/urlSearchParams';
import { CounterOptions } from 'src/utils/counterOptions';
import { setDefer } from 'src/utils/defer';
import { bindArgs } from 'src/utils/function';
Expand All @@ -16,15 +13,15 @@ import { DEFAULT_COUNTER_TYPE } from '../counterOptions';
export const CHK_STATUS_KEY = 'cs';

export const checkStatusRaw = (ctx: Window, counterOptions: CounterOptions) => {
const id = counterIdForCheck(ctx);
const { id, lang } = getStatusCheckSearchParams(ctx);

if (
id &&
counterOptions.id === id &&
counterOptions.counterType === DEFAULT_COUNTER_TYPE
) {
const src = getResourceUrl(ctx, {
['lang']: langForCheck(ctx),
['lang']: lang,
['fileId']: 'status',
});
setDefer(
Expand Down
4 changes: 2 additions & 2 deletions src/providers/statusCheck/statusCheckFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ExportedCounterInfo,
GetCountersMethod,
} from 'src/providers/getCounters/types';
import { counterIdForCheck } from './urlSearchParams';
import { getStatusCheckSearchParams } from './urlSearchParams';

/** Result of checking if counter exists on page */
interface CheckStatusResult {
Expand All @@ -19,7 +19,7 @@ interface CheckStatusResult {
}

export const checkStatusFn = (ctx: Window): CheckStatusResult => {
const id = counterIdForCheck(ctx);
const { id } = getStatusCheckSearchParams(ctx);

const globalConfig = getGlobalStorage(ctx);
const getCountersFn = globalConfig.getVal<GetCountersMethod>(
Expand Down
34 changes: 12 additions & 22 deletions src/providers/statusCheck/urlSearchParams.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { parse } from 'src/utils/querystring';
import { memo, pipe } from 'src/utils/function';
import { memo } from 'src/utils/function';
import { getLocation } from 'src/utils/location';
import { ctxPath } from 'src/utils/object';
import { parseDecimalInt } from 'src/utils/number';

export const CHECK_URL_PARAM = '_ym_status-check';
Expand All @@ -12,28 +11,19 @@ export const DEFAULT_LANGUAGE = 'ru';
/** Search parameters values */
interface StatusCheckSearchParams {
/** Status check */
[CHECK_URL_PARAM]: string;
id: number;
/** Language */
[LANG_URL_PARAM]: string;
lang: string;
}

const getSearchParams = memo((ctx: Window) => {
const location = getLocation(ctx);
const searchParams: Partial<StatusCheckSearchParams> = parse(
location.search.substring(1),
);
export const getStatusCheckSearchParams = memo(
(ctx: Window): StatusCheckSearchParams => {
const location = getLocation(ctx);
const searchParams = parse(location.search.substring(1));

searchParams[CHECK_URL_PARAM] = searchParams[CHECK_URL_PARAM] || '';
searchParams[LANG_URL_PARAM] =
searchParams[LANG_URL_PARAM] || DEFAULT_LANGUAGE;

return searchParams as StatusCheckSearchParams;
});

export const counterIdForCheck = pipe(
getSearchParams,
ctxPath(CHECK_URL_PARAM),
parseDecimalInt,
return {
id: parseDecimalInt(searchParams[CHECK_URL_PARAM] || ''),
lang: searchParams[LANG_URL_PARAM] || DEFAULT_LANGUAGE,
};
},
);

export const langForCheck = pipe(getSearchParams, ctxPath(LANG_URL_PARAM));

0 comments on commit f4f2678

Please sign in to comment.