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

Add jest round2 #37

Merged
Merged
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
7 changes: 7 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ plugins:
- react
- jsx-a11y
- react-hooks
- jest
env:
node: true
browser: true
jest: true

parser: babel-eslint

Expand All @@ -25,3 +27,8 @@ rules:
react/state-in-constructor: 0
react-hooks/rules-of-hooks: "error"
react-hooks/exhaustive-deps: "warn"
jest/no-disabled-tests: "warn"
jest/no-focused-tests: "error"
jest/no-identical-title: "error"
jest/prefer-to-have-length: "warn"
jest/valid-expect: "error"
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ lint:

make lint-fix:
npx eslint . --fix

test:
npx jest

test-watch:
npx jest --watch
5 changes: 5 additions & 0 deletions __fixtures__/getRemoveResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getRemoveResponse = gameId => ({
data: JSON.stringify([null, null, 'lobby', 'game:remove', { id: gameId }]),
});

export default getRemoveResponse;
36 changes: 36 additions & 0 deletions __fixtures__/getUpdateResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const getUpdateResponse = (gameId, state = 'waiting_opponent') => ({
data: JSON.stringify([null,
null,
'lobby',
'game:upsert',
{
game: {
id: gameId,
inserted_at: '2020-10-04T22:38:22',
is_bot: false,
level: 'elementary',
players: [{
id: 2782,
name: 'H9ko',
is_bot: false,
github_id: 57991929,
lang: 'js',
editor_text: 'module.exports = () => {\n\n};',
editor_lang: 'js',
creator: true,
game_result: 'undefined',
check_result: {
asserts: [], asserts_count: 0, output: '', result: '{"status": "info"}', status: 'initial', success_count: 0,
},
achievements: ['played_ten_games', 'played_fifty_games'],
rating: 1152,
rating_diff: 0,
}],
state,
timeout_seconds: 7200,
type: 'public',
},
}]),
});

export default getUpdateResponse;
159 changes: 159 additions & 0 deletions __fixtures__/phxReply4bots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
export default {
data: JSON.stringify([
'7',
'7',
'lobby',
'phx_reply',
{
response: {
active_games: [
{
id: 19887,
inserted_at: '2020-09-27T13:30:54',
is_bot: true,
level: 'medium',
players: [
{
id: -27,
name: 'HappyQleaner',
is_bot: true,
github_id: 35539033,
lang: 'js',
editor_text: 'module.exports = () => {\n\n};',
editor_lang: 'js',
creator: true,
game_result: 'undefined',
check_result: {
asserts: [],
asserts_count: 0,
output: '',
result: '{"status": "info"}',
status: 'initial',
success_count: 0,
},
achievements: [
'played_hundred_games',
'win_games_with?clojure_cpp_csharp_golang_haskell_js_kotlin_php_python_ruby',
],
rating: 1365,
rating_diff: 0,
},
],
state: 'waiting_opponent',
timeout_seconds: 3600,
type: 'bot',
},
{
id: 19923,
inserted_at: '2020-09-28T12:17:11',
is_bot: true,
level: 'hard',
players: [
{
id: -18,
name: 'DenisPython',
is_bot: true,
github_id: 35539033,
lang: 'js',
editor_text: 'module.exports = () => {\n\n};',
editor_lang: 'js',
creator: true,
game_result: 'undefined',
check_result: {
asserts: [],
asserts_count: 0,
output: '',
result: '{"status": "info"}',
status: 'initial',
success_count: 0,
},
achievements: [
'played_hundred_games',
'win_games_with?cpp_csharp_elixir_golang_haskell_java_js_php_python_ruby_ts',
],
rating: 1344,
rating_diff: 0,
},
],
state: 'waiting_opponent',
timeout_seconds: 3600,
type: 'bot',
},
{
id: 20011,
inserted_at: '2020-09-30T16:15:49',
is_bot: true,
level: 'elementary',
players: [
{
id: -9,
name: 'UlaBack',
is_bot: true,
github_id: 35539033,
lang: 'ruby',
editor_text: 'module.exports = () => {\n\n};',
editor_lang: 'ruby',
creator: true,
game_result: 'undefined',
check_result: {
asserts: [],
asserts_count: 0,
output: '',
result: '{"status": "info"}',
status: 'initial',
success_count: 0,
},
achievements: [
'played_hundred_games',
'win_games_with?cpp_csharp_elixir_haskell_java_js_php_python_ruby',
],
rating: 1261,
rating_diff: 0,
},
],
state: 'waiting_opponent',
timeout_seconds: 3600,
type: 'bot',
},
{
id: 20013,
inserted_at: '2020-09-30T17:14:20',
is_bot: true,
level: 'easy',
players: [
{
id: -5,
name: 'ValyaFront',
is_bot: true,
github_id: 35539033,
lang: 'js',
editor_text: 'module.exports = () => {\n\n};',
editor_lang: 'js',
creator: true,
game_result: 'undefined',
check_result: {
asserts: [],
asserts_count: 0,
output: '',
result: '{"status": "info"}',
status: 'initial',
success_count: 0,
},
achievements: [
'played_hundred_games',
'win_games_with?clojure_cpp_elixir_golang_java_js_php_python_ruby',
],
rating: 1287,
rating_diff: 0,
},
],
state: 'waiting_opponent',
timeout_seconds: 3600,
type: 'bot',
},
],
},
status: 'ok',
},
]),
};
46 changes: 46 additions & 0 deletions __test__/background.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import browser from 'webextension-polyfill';
import socketConnect from '../src/background/socket';
import gameStatuses from '../src/background/models';
import phxReply4bots from '../__fixtures__/phxReply4bots';
import getUpdateResponseWithID from '../__fixtures__/getUpdateResponse';
import getRemoveResponseWithID from '../__fixtures__/getRemoveResponse';

describe('socket', () => {
const fakeSocket = {};
socketConnect(('wss://codebattle.hexlet.io/extension/websocket?vsn=2.0.0'), fakeSocket);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если честно, я пока не понял как эта штука сработала.
Ощущение что либо у нас неправильно сделан socketConnect, либо где-то идёт мухлеж с кодом

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ну не мухлеж... Все по заветам хекслета. Это идея из продвинутого тестирования. Сущность сокета по умолчанию устанавливается, и если явно не передать в socketConnect


test('phxReply', () => {
fakeSocket.onmessage(phxReply4bots);
expect(browser.browserAction.setBadgeText).toHaveBeenLastCalledWith({
text: null,
});
});
test('add games->remove games', () => {
fakeSocket.onmessage(getUpdateResponseWithID(1000, gameStatuses.waiting));
expect(browser.browserAction.setBadgeText).toHaveBeenLastCalledWith({
text: '1',
});
fakeSocket.onmessage(getUpdateResponseWithID(1001, gameStatuses.waiting));
expect(browser.browserAction.setBadgeText).toHaveBeenLastCalledWith({
text: '2',
});
fakeSocket.onmessage(getRemoveResponseWithID(1001));
expect(browser.browserAction.setBadgeText).toHaveBeenLastCalledWith({
text: '1',
});
fakeSocket.onmessage(getRemoveResponseWithID(1000));
expect(browser.browserAction.setBadgeText).toHaveBeenLastCalledWith({
text: null,
});
});
test('add game -> changeStatus', () => {
fakeSocket.onmessage(getUpdateResponseWithID(1002, gameStatuses.waiting));
expect(browser.browserAction.setBadgeText).toHaveBeenLastCalledWith({
text: '1',
});
fakeSocket.onmessage(getUpdateResponseWithID(1002, gameStatuses.joined));
expect(browser.browserAction.setBadgeText).toHaveBeenLastCalledWith({
text: null,
});
});
});
Loading