Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Разделены тесты #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable camelcase */
/// <reference types="cypress" />

import { createAssistant } from 'lib';
import { Hints, Suggestions } from '@salutejs/scenario';

import { SystemMessageHeaderByttonsType } from '../../src/index';
import { SystemMessageHeaderByttonsType, createAssistant as createAssistantT } from '../../src/index';

/* eslint-disable @typescript-eslint/camelcase */
declare type createAssistant = typeof createAssistantT;

describe('Проверяем createAssistant', () => {
beforeEach(() => {
Expand Down Expand Up @@ -57,16 +59,18 @@ describe('Проверяем createAssistant', () => {
expectedName: string | null,
) =>
new Promise((resolve) => {
window.AssistantHost.sendData = (action: string, name: string | null) => {
expect(action).not.undefined;
expect(action).not.empty;
expect(expectedAction).to.deep.equal(JSON.parse(action));
window.AssistantHost.sendData = (act: string, name: string | null) => {
expect(act).not.undefined;
expect(act).not.empty;
expect(expectedAction).to.deep.equal(JSON.parse(act));

if (expectedName) {
expect(expectedName).to.equal(name);
} else {
expect(name).null; // отправляем null, вместо undefined
}
resolve();

resolve(undefined);
};

assistant.sendData({ action: expectedAction, name: expectedName });
Expand All @@ -81,17 +85,17 @@ describe('Проверяем createAssistant', () => {
new Promise((resolve) => {
window.AssistantHost.sendData = cy.stub();
window.AssistantHost.sendDataContainer = (container: string) => {
const { data, message_name, requestId } = JSON.parse(container);
const { data, message_name, requestId: reqId } = JSON.parse(container);

expect(expectedAction).to.deep.equal(data);
expect(expectedRequestId).to.equal(requestId);
expect(expectedRequestId).to.equal(reqId);
if (expectedName) {
expect(expectedName).to.equal(message_name);
} else {
expect(message_name).empty; // отправляем пустую строку, вместо undefined
}

resolve();
resolve(undefined);
};

assistant.sendData({ action: expectedAction, name: expectedName, requestId: expectedRequestId });
Expand Down Expand Up @@ -155,8 +159,8 @@ describe('Проверяем createAssistant', () => {

// не передаем requestId, ожидаем ответ
window.AssistantHost.sendDataContainer = (data) => {
const { requestId } = JSON.parse(data);
setTimeout(() => window.AssistantClient.onData({ ...command, sdk_meta: { requestId } }));
const { requestId: reqId } = JSON.parse(data);
setTimeout(() => window.AssistantClient.onData({ ...command, sdk_meta: { requestId: reqId } }));
};
assistant.sendData({ action }, ({ sdk_meta, ...cmd }) => {
expect(cmd).to.deep.equal(command);
Expand Down Expand Up @@ -332,12 +336,12 @@ describe('Проверяем createAssistant', () => {
let callbackHadCommand = false;

window.AssistantHost.sendDataContainer = (data) => {
const { requestId } = JSON.parse(data);
const { requestId: reqId } = JSON.parse(data);

window.setTimeout(() => {
window.AssistantClient.onData({
type: 'smart_app_data',
sdk_meta: { requestId },
sdk_meta: { requestId: reqId },
smart_app_data: testResponse,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Проверяем createAssistantDev', () => {
let phraseReceived: boolean;
let isDone = false;

server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
initProtocol(socket);

Expand All @@ -53,7 +53,7 @@ describe('Проверяем createAssistantDev', () => {
if (message.initialSettings) {
settingsReceived =
message.initialSettings.userChannel === USER_CHANNEL &&
message.initialSettings.device.surface === SURFACE &&
message.initialSettings.device?.surface === SURFACE &&
message.initialSettings.device.platformType === 'WEBDBG';
}

Expand Down Expand Up @@ -87,9 +87,13 @@ describe('Проверяем createAssistantDev', () => {
smart_app_data: { type: 'test_command' },
};
const FEATURES = { feature: true };
server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
initProtocol(socket, { initPhrase: INIT_PHRASE, items: [{ command: COMMAND }], systemMessage: { feature_launcher: FEATURES } });
initProtocol(socket, {
initPhrase: INIT_PHRASE,
items: [{ command: COMMAND }],
systemMessage: { feature_launcher: FEATURES },
});
});

const assistant = createAssistantDev<AssistantSmartAppCommand>({
Expand Down Expand Up @@ -139,9 +143,13 @@ describe('Проверяем createAssistantDev', () => {
smart_app_data: { type: 'test_command' },
};
const FEATURES = { feature: true };
server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
initProtocol(socket, { initPhrase: INIT_PHRASE, items: [{ command: COMMAND }], systemMessage: { feature_launcher: FEATURES } });
initProtocol(socket, {
initPhrase: INIT_PHRASE,
items: [{ command: COMMAND }],
systemMessage: { feature_launcher: FEATURES },
});
});

const assistant = createAssistantDev<AssistantSmartAppCommand>({
Expand Down Expand Up @@ -182,7 +190,7 @@ describe('Проверяем createAssistantDev', () => {
});

it('Проверяем оповещение подписчиков о старте', (done) => {
server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
initProtocol(socket, { initPhrase: INIT_PHRASE });
});
Expand All @@ -201,7 +209,7 @@ describe('Проверяем createAssistantDev', () => {
it('Проверяем вызов send_data - ожидаем мессадж с app_info и стейтом в нем', (done) => {
const state = { item_selector: { items: [] }, key: 'TEST' };

server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
initProtocol(socket, { initPhrase: INIT_PHRASE });

Expand All @@ -219,7 +227,8 @@ describe('Проверяем createAssistantDev', () => {
expect(server_action).to.ok;
expect(message.meta).to.ok;

switch (data.server_action.action_id) {
// @ts-ignore
switch (data.server_action?.action_id) {
case 'first':
expect(current_app.app_info).to.deep.equal(APP_INFO);
expect(current_app.state).to.deep.equal(state);
Expand Down Expand Up @@ -248,7 +257,7 @@ describe('Проверяем createAssistantDev', () => {
it('Проверяем подписку sendAction - ожидаем срабатывание подписки', (done) => {
const smartAppData = { type: 'test_data' };

server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
initProtocol(socket, { initPhrase: INIT_PHRASE });

Expand All @@ -258,6 +267,7 @@ describe('Проверяем createAssistantDev', () => {
if (message.systemMessage?.data && message.systemMessage?.data !== '{}') {
const { server_action }: SystemMessageDataType = JSON.parse(message.systemMessage.data);

// @ts-ignore
if (server_action?.type !== 'test_action') {
return;
}
Expand Down Expand Up @@ -298,7 +308,7 @@ describe('Проверяем createAssistantDev', () => {
const received = { character: false, navigation: false, data: false, insets: false };
let handleStart: () => void;

server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
initProtocol(socket, {
initPhrase: INIT_PHRASE,
Expand Down Expand Up @@ -356,7 +366,7 @@ describe('Проверяем createAssistantDev', () => {
const characterId = 'joy';
let handleStart: () => void;

server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
initProtocol(socket, { initPhrase: INIT_PHRASE });

Expand Down Expand Up @@ -392,7 +402,7 @@ describe('Проверяем createAssistantDev', () => {
it('Проверяем восстановление recoveryState', (done) => {
const recoveryState = { type: 'recovery_state', item_selector: { items: [] } };

server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
initProtocol(socket, { initPhrase: INIT_PHRASE });
});
Expand Down Expand Up @@ -431,7 +441,7 @@ describe('Проверяем createAssistantDev', () => {

cy.stub(window.history, 'back', historyBack);

server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
setTimeout(() =>
socket.send(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ import { createAssistant } from 'lib';
describe('Проверяем createAssistant на вебе (без сдк)', () => {
const state = { type: 'test app state' };
const recoveryState = { type: 'test app recovery state' };
let expectAfterIt = undefined;
let expectAfterIt: (() => void) | undefined;

before(() => {
createAssistant({ getState: () => ({}) });
window.AssistantClient = {
onData: cy.stub(),
onStart: cy.stub(),
onRequestState: cy.stub().returns(state),
onRequestRecoveryState: cy.stub().returns(recoveryState)
onRequestRecoveryState: cy.stub().returns(recoveryState),
};
});

beforeEach(() => {
expectAfterIt = undefined;
window.top.postMessage = cy.stub();
})
if (window.top) {
window.top.postMessage = cy.stub();
}
});

afterEach(() => {
expectAfterIt && expectAfterIt();
})
expectAfterIt?.();
});

it('appInitialData и AssistantHost установлены', () => {
expect(window.appInitialData).to.deep.equals([]);
Expand All @@ -32,28 +34,31 @@ describe('Проверяем createAssistant на вебе (без сдк)', ()

it('Проксирование onStart', () => {
window.postMessage(JSON.stringify({ type: 'onStart' }), '*');
cy.wrap(window.AssistantClient.onStart).should(onStart => expect(onStart).to.calledOnce);
cy.wrap(window.AssistantClient?.onStart).should((onStart) => expect(onStart).to.calledOnce);
});

it('Проксирование onData', () => {
const payload = { type: 'test' };

window.postMessage(JSON.stringify({ type: 'onData', payload }), '*');
cy.wrap(window.AssistantClient.onData).should(onData => expect(onData).to.calledOnceWith(payload));
cy.wrap(window.AssistantClient?.onData).should((onData) => expect(onData).to.calledOnceWith(payload));
});

it('Реакция onRequestState', () => {
expectAfterIt = () => {
expect(window.AssistantClient.onRequestState).to.calledOnce;
expect(window.top.postMessage).to.calledOnceWith(JSON.stringify({ type: 'state', payload: state }), '*');
expect(window.AssistantClient?.onRequestState).to.calledOnce;
expect(window.top?.postMessage).to.calledOnceWith(JSON.stringify({ type: 'state', payload: state }), '*');
};
window.postMessage(JSON.stringify({ type: 'onRequestState' }), '*');
});

it('Реакция onRequestRecoveryState', () => {
expectAfterIt = () => {
expect(window.AssistantClient.onRequestRecoveryState).to.calledOnce;
expect(window.top.postMessage).to.calledOnceWith(JSON.stringify({ type: 'recoveryState', payload: recoveryState }), '*');
expect(window.AssistantClient?.onRequestRecoveryState).to.calledOnce;
expect(window.top?.postMessage).to.calledOnceWith(
JSON.stringify({ type: 'recoveryState', payload: recoveryState }),
'*',
);
};
window.postMessage(JSON.stringify({ type: 'onRequestRecoveryState' }), '*');
});
Expand All @@ -62,9 +67,11 @@ describe('Проверяем createAssistant на вебе (без сдк)', ()
window.history.back = cy.stub();
expectAfterIt = () => {
expect(window.history.back).to.calledOnce;
cy.wrap(window.top.postMessage, { timeout: 600 }).should(func => expect(func).to.calledOnceWith(JSON.stringify({ type: 'close' }), '*'));
cy.wrap(window.top?.postMessage, { timeout: 600 }).should((func) =>
expect(func).to.calledOnceWith(JSON.stringify({ type: 'close' }), '*'),
);
};

window.postMessage(JSON.stringify({ type: 'onBack' }), '*');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable camelcase */
/// <reference types="cypress" />
import { WebSocket, Server } from 'mock-socket';

Expand Down Expand Up @@ -35,7 +36,7 @@ describe('Проверяем', () => {
let context: Message;
let doneCalled = false;

server.on('connection', (socket) => {
server?.on('connection', (socket) => {
socket.binaryType = 'arraybuffer';
initProtocol(socket, {
initPhrase: INIT_PHRASE,
Expand All @@ -53,7 +54,6 @@ describe('Проверяем', () => {
}

if (action && context && !doneCalled) {
// eslint-disable-next-line @typescript-eslint/camelcase
const current_app = JSON.parse(context.meta.current_app);
doneCalled = true;

Expand Down
Loading
Loading