Skip to content

Commit

Permalink
Delete phone hide preview
Browse files Browse the repository at this point in the history
The feature is deprecated
b1460a654e3b7a0591996b85de7f5c2ec0f8fda5
  • Loading branch information
sabio committed May 30, 2024
1 parent 66a27ee commit ff6d1be
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 113 deletions.
121 changes: 51 additions & 70 deletions src/providers/remoteControl/__tests__/inline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,59 @@ import {
remoteControl,
UTILS_CLOSEST_KEY,
UTILS_GET_DATA_KEY,
UTILS_HIDE_PHONES_KEY,
UTILS_KEY,
UTILS_SELECT_KEY,
} from 'src/providers/remoteControl/remoteControl';
import * as events from 'src/utils/events';
import * as domUtils from 'src/utils/dom';
import type { EventSetter } from 'src/utils/events/types';
import * as globalUtils from 'src/storage/global';
import * as functionUtils from 'src/utils/function';
import type { AnyFunc } from 'src/utils/function/types';
import type { GlobalStorage } from 'src/storage/global';

describe('remoteControl / inline', () => {
const sandbox = sinon.createSandbox();

let eventHandlerOn: sinon.SinonStub;
let eventHandlerUn: sinon.SinonStub;
let testEventData = {};
let insertScriptStub: sinon.SinonStub;
let setValSpy: sinon.SinonSpy;
let bindArgsStub: sinon.SinonStub;
let eventHandlerOn: sinon.SinonStub<
Parameters<EventSetter['on']>,
ReturnType<EventSetter['on']>
>;
let eventHandlerUn: sinon.SinonStub<
Parameters<EventSetter['un']>,
ReturnType<EventSetter['un']>
>;
let insertScriptStub: sinon.SinonStub<
Parameters<typeof domUtils.insertScript>,
ReturnType<typeof domUtils.insertScript>
>;
let setValSpy: sinon.SinonSpy<
Parameters<globalUtils.GlobalStorage['setVal']>,
ReturnType<globalUtils.GlobalStorage['setVal']>
>;

const metrikaOrigin = 'https://metrika.example.com';
const externalOrigin = 'https://iframe-toloka.com';

const windowStub = {
JSON,
} as unknown as Window;

beforeEach(() => {
eventHandlerOn = sinon
.stub()
.callsFake((ctx: Window, event: string, cb: AnyFunc) => {
cb(testEventData);
});
eventHandlerOn = sinon.stub();
eventHandlerUn = sinon.stub();

sandbox.stub(events, 'cEvent').returns({
on: eventHandlerOn,
un: eventHandlerUn,
});

setValSpy = sandbox.spy();
} as EventSetter);

insertScriptStub = sandbox.stub(domUtils, 'insertScript');

setValSpy = sandbox.spy(
(name: string, value: unknown) => ({} as GlobalStorage),
);
sandbox.stub(globalUtils, 'getGlobalStorage').returns({
setVal: setValSpy,
setSafe: sandbox.spy(),
getVal: sandbox.spy(),
});

bindArgsStub = sandbox.stub(functionUtils, 'bindArgs');
});

afterEach(() => {
Expand All @@ -64,47 +67,37 @@ describe('remoteControl / inline', () => {
const createResourcePath = (entity: string) =>
`https://yastatic.net/s3/metrika/1.2.3/form-selector/${entity}_ru.js`;

const createMessageData = (
fileId: string,
data?: string,
origin: string = metrikaOrigin,
) => ({
origin,
data: JSON.stringify({
id: `${fileId}-new-id`,
action: 'appendremote',
version: '3',
inline: true,
data,
lang: 'ru',
appVersion: '1.2.3',
fileId,
}),
});
const createMessageData = (fileId: string) =>
({
origin: metrikaOrigin,
data: JSON.stringify({
id: `${fileId}-new-id`,
action: 'appendremote',
version: '3',
inline: true,
lang: 'ru',
appVersion: '1.2.3',
fileId,
}),
} as MessageEvent<string>);

const checkUtils = (entityKey: string) => {
const setValSpyCall = setValSpy.getCall(1);
sinon.assert.calledWith(setValSpyCall, UTILS_KEY);

const utils = setValSpyCall.args[1][entityKey];

chai.expect(utils).to.have.property(UTILS_CLOSEST_KEY);
chai.expect(utils).to.have.property(UTILS_SELECT_KEY);
chai.expect(utils).to.have.property(UTILS_GET_DATA_KEY);
};

const checkHidePhones = (phones: string[]) => {
const setValSpyCall = setValSpy.getCall(1);
sinon.assert.calledWith(setValSpyCall, UTILS_KEY);
sinon.assert.calledWith(setValSpyCall, UTILS_KEY, sinon.match.object);

const utils = setValSpyCall.args[1].phone;
chai.expect(utils).to.have.property(UTILS_HIDE_PHONES_KEY);
const utils = setValSpyCall.args[1] as Record<string, unknown>;
const entityUtils = utils[entityKey];

sinon.assert.calledWith(bindArgsStub, [windowStub, null, phones]);
chai.expect(entityUtils).to.have.property(UTILS_CLOSEST_KEY);
chai.expect(entityUtils).to.have.property(UTILS_SELECT_KEY);
chai.expect(entityUtils).to.have.property(UTILS_GET_DATA_KEY);
};

it('select form', () => {
testEventData = createMessageData('form');
eventHandlerOn.callsFake((ctx, _events, cb) => {
cb.call(ctx, createMessageData('form'));
return () => {};
});

remoteControl(windowStub);

Expand All @@ -115,7 +108,10 @@ describe('remoteControl / inline', () => {
});

it('select button', () => {
testEventData = createMessageData('button');
eventHandlerOn.callsFake((ctx, _events, cb) => {
cb.call(ctx, createMessageData('button'));
return () => {};
});

remoteControl(windowStub);

Expand All @@ -124,19 +120,4 @@ describe('remoteControl / inline', () => {
});
checkUtils('button');
});

it('hide phones - exact phone', () => {
testEventData = createMessageData(
'phone',
'89995556677',
externalOrigin,
);

remoteControl(windowStub);

sinon.assert.calledWith(insertScriptStub, windowStub, {
src: createResourcePath('phone'),
});
checkHidePhones(['89995556677']);
});
});
54 changes: 27 additions & 27 deletions src/providers/remoteControl/__tests__/remoteControl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as chai from 'chai';
import Sinon, * as sinon from 'sinon';
import * as sinon from 'sinon';
import { yaNamespace } from 'src/const';
import { metrikaNamespace } from 'src/storage/global';
import * as functionUtils from 'src/utils/function';
import * as events from 'src/utils/events';
import * as global from 'src/storage/global';
import type { EventSetter } from 'src/utils/events/types';
import * as fnv32a from 'src/utils/fnv32a';
import * as inject from '@inject';
import { LOCAL_FEATURE } from 'generated/features';
Expand Down Expand Up @@ -123,7 +124,7 @@ describe('getResourceUrl', () => {
});

it('Only allowed ids', () => {
['button', 'form', 'phone'].forEach((fileId) => {
['button', 'form'].forEach((fileId) => {
chai.expect(
remoteControl.getResourceUrl({
lang: 'ru',
Expand Down Expand Up @@ -201,8 +202,8 @@ describe('getResourceUrl', () => {

describe('remoteControl/onMessage', () => {
const sandbox = sinon.createSandbox();
const ctx = { JSON } as unknown as Window;
let handleMessageStub: Sinon.SinonStub<
const ctx = { JSON } as Window;
let handleMessageStub: sinon.SinonStub<
[ctx: Window, event: MessageEvent, message: remoteControl.Message],
void
>;
Expand All @@ -222,52 +223,56 @@ describe('remoteControl/onMessage', () => {
remoteControl.onMessage(ctx, {
data: JSON.stringify({ action: 'something_wrong' }),
origin: 'https://metrika.yandex.com',
} as unknown as MessageEvent);
} as MessageEvent);
sinon.assert.notCalled(handleMessageStub);
});

it('handles events from valid metrika origins', () => {
const message = {
action: 'appendremote',
} as unknown as remoteControl.Message;
} as remoteControl.Message;
const origin = 'https://metrika.yandex.com';
const event = {
data: JSON.stringify(message),
origin,
} as unknown as MessageEvent;
} as MessageEvent;
remoteControl.onMessage(ctx, event);
sinon.assert.calledWith(handleMessageStub, ctx, event, message);
});
});

describe('remoteControl', () => {
const eventHandlerUnsubscribe = sinon.stub();
const eventHandlerOn = sinon.stub().returns(eventHandlerUnsubscribe);
const eventHandlerOn = sinon
.stub<Parameters<EventSetter['on']>, ReturnType<EventSetter['on']>>()
.returns(eventHandlerUnsubscribe);
const eventHandlerUn = sinon.stub();
const getGlobalValue = sinon.stub();
const setGlobalValue = sinon.stub();
const hashResult = 100;
const sandbox = sinon.createSandbox();

let cEvent: any;
let cEvent: sinon.SinonStub<
Parameters<typeof events.cEvent>,
ReturnType<typeof events.cEvent>
>;

beforeEach(() => {
sandbox.stub(fnv32a, 'fnv32a').returns(hashResult);
sandbox
.stub(functionUtils, 'bindArg')
.callsFake((arg: any, callback: (...args: any[]) => any) => {
return callback;
});
.callsFake((arg, callback) => callback);
getGlobalValue.withArgs(remoteControl.REMOTE_CONTROL).returns(false);
sandbox.stub(global, 'getGlobalStorage').returns({
getVal: getGlobalValue,
setSafe: setGlobalValue,
setVal: setGlobalValue,
} as any);
cEvent = sandbox.stub(events, 'cEvent').returns({
});
cEvent = sandbox.stub(events, 'cEvent');
cEvent.returns({
on: eventHandlerOn,
un: eventHandlerUn,
});
} as EventSetter);
});

afterEach(() => {
Expand All @@ -284,27 +289,22 @@ describe('remoteControl', () => {
[yaNamespace]: {
[metrikaNamespace]: {},
},
} as unknown as Window;
const errorMessage = 'addEventListener was called with wrong arguments';
} as Window;

remoteControl.remoteControl(windowStub);

chai.expect(cEvent.called).to.be.true;
chai.expect(
eventHandlerOn.getCall(0).args[1],
errorMessage,
).to.deep.equal(['message']);
chai.expect(eventHandlerOn.getCall(0).args[2], errorMessage).to.equal(
sinon.assert.calledOnceWithExactly(
eventHandlerOn,
windowStub,
['message'],
remoteControl.onMessage,
);

getGlobalValue.withArgs(remoteControl.REMOTE_CONTROL).returns(true);

remoteControl.remoteControl(windowStub);
remoteControl.remoteControl(windowStub);

chai.assert(
eventHandlerOn.calledOnce,
'addEventListener should be called only once',
);
sinon.assert.calledOnce(eventHandlerOn);
});
});
19 changes: 3 additions & 16 deletions src/providers/remoteControl/remoteControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { getGlobalStorage } from 'src/storage/global';
import {
CLICK_TRACKING_FEATURE,
HIDE_PHONES_FEATURE,
LOCAL_FEATURE,
SUBMIT_TRACKING_FEATURE,
CHECK_STATUS_FEATURE,
Expand Down Expand Up @@ -37,7 +36,6 @@ import {
} from 'src/utils/array';
import { closestButton, selectButtons } from 'src/utils/dom/button';
import { closestForm, getFormData, selectForms } from 'src/utils/dom/form';
import { hidePhones } from 'src/utils/phones/phonesHide';
import { checkStatusFn } from 'src/providers/statusCheck/statusCheckFn';
import { parseDecimalInt } from 'src/utils/number';
import { AnyFunc } from 'src/utils/function/types';
Expand Down Expand Up @@ -158,11 +156,10 @@ export const UTILS_CLOSEST_KEY = 'closest';
export const UTILS_SELECT_KEY = 'select';
export const UTILS_GET_DATA_KEY = 'getData';

export const UTILS_HIDE_PHONES_KEY = 'hidePhones';
export const UTILS_CHECK_STATUS_KEY = 'checkStatus';

const SPLITTER = '.';
const AVAILABLE_FILES = ['form', 'button', 'phone', 'status'];
const AVAILABLE_FILES = ['form', 'button', 'status'];

const BETA_URL = 'https://s3.mds.yandex.net/internal-metrika-betas';
const URL = 'https://yastatic.net/s3/metrika';
Expand Down Expand Up @@ -204,12 +201,10 @@ export const setupUtilsAndLoadScript = (
ctx: ExtendedWindow,
src?: string,
counterId = '',
phones = '',
) => {
if (
flags[CLICK_TRACKING_FEATURE] ||
flags[SUBMIT_TRACKING_FEATURE] ||
flags[HIDE_PHONES_FEATURE] ||
flags[REMOTE_CONTROL_FEATURE] ||
flags[CHECK_STATUS_FEATURE]
) {
Expand All @@ -234,14 +229,6 @@ export const setupUtilsAndLoadScript = (
[UTILS_GET_DATA_KEY]: bindArg(ctx, getButtonData),
};
}
if (flags[HIDE_PHONES_FEATURE] || flags[REMOTE_CONTROL_FEATURE]) {
utils['phone'] = {
[UTILS_HIDE_PHONES_KEY]: bindArgs(
[ctx, null, [phones]],
hidePhones,
),
};
}
if (flags[CHECK_STATUS_FEATURE]) {
utils['status'] = {
[UTILS_CHECK_STATUS_KEY]: bindArgs(
Expand Down Expand Up @@ -269,8 +256,8 @@ export const handleMessage = memo(
cForEach(pipe(ctxBindArgs(args), call), REMOTE_CONTROL_LISTENERS);
if (message['inline']) {
const src = getResourceUrl(message);
const { ['data']: data = '', id = '' } = message;
setupUtilsAndLoadScript(ctx, src, id, data);
const { id = '' } = message;
setupUtilsAndLoadScript(ctx, src, id);
} else if (
message['resource'] &&
isAllowedResource(message['resource'])
Expand Down

0 comments on commit ff6d1be

Please sign in to comment.