From 27ea00e152a202fc983ae1c4cb4962c3630e82d2 Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Fri, 19 Apr 2019 10:58:21 -0400 Subject: [PATCH] Fix some frontend linting issues (#2495) [ci skip] --- app/javascript/.eslintrc.js | 3 ++ .../elements/__mocks__/algoliasearch.js | 8 ++--- .../chat/__mocks__/algoliasearchUsers.js | 4 +-- app/javascript/chat/actions.js | 6 ++-- .../src/utils/__tests__/getUserData.test.js | 30 ------------------- app/javascript/src/utils/getUserData.js | 23 -------------- app/javascript/src/utils/pusher.js | 28 ++++++++--------- 7 files changed, 26 insertions(+), 76 deletions(-) delete mode 100644 app/javascript/src/utils/__tests__/getUserData.test.js delete mode 100644 app/javascript/src/utils/getUserData.js diff --git a/app/javascript/.eslintrc.js b/app/javascript/.eslintrc.js index 989ef84ce..c27e96f7f 100644 --- a/app/javascript/.eslintrc.js +++ b/app/javascript/.eslintrc.js @@ -22,9 +22,12 @@ module.exports = { }, ], 'import/prefer-default-export': 'off', + 'no-unused-vars': ['error', { argsIgnorePattern: '^_' }], }, globals: { InstantClick: false, filterXSS: false, + Pusher: false, + algoliasearch: false, }, }; diff --git a/app/javascript/article-form/elements/__mocks__/algoliasearch.js b/app/javascript/article-form/elements/__mocks__/algoliasearch.js index 8557d1e11..0a49238a6 100644 --- a/app/javascript/article-form/elements/__mocks__/algoliasearch.js +++ b/app/javascript/article-form/elements/__mocks__/algoliasearch.js @@ -4,8 +4,8 @@ global.document.head.innerHTML = ""; const mockIndex = { - search: (query, options) => - new Promise((resolve, reject) => { + search: (query, _options) => + new Promise((resolve, _reject) => { process.nextTick(() => { const searchResults = { gi: { @@ -56,7 +56,7 @@ const mockIndex = { }), }; const client = { - initIndex: index => mockIndex, + initIndex: _index => mockIndex, }; -export default jest.fn().mockImplementation((id, key) => client); +export default jest.fn().mockImplementation((_id, _key) => client); diff --git a/app/javascript/chat/__mocks__/algoliasearchUsers.js b/app/javascript/chat/__mocks__/algoliasearchUsers.js index f87c75788..813a229db 100644 --- a/app/javascript/chat/__mocks__/algoliasearchUsers.js +++ b/app/javascript/chat/__mocks__/algoliasearchUsers.js @@ -36,7 +36,7 @@ const mockIndex = { }), }; const client = { - initIndex: index => mockIndex, // eslint-ignore-line + initIndex: _index => mockIndex, // eslint-ignore-line }; -export default jest.fn().mockImplementation((id, key) => client); // eslint-ignore-line +export default jest.fn().mockImplementation((_id_, _key) => client); // eslint-ignore-line diff --git a/app/javascript/chat/actions.js b/app/javascript/chat/actions.js index 3b2018501..a61d209fe 100644 --- a/app/javascript/chat/actions.js +++ b/app/javascript/chat/actions.js @@ -79,7 +79,7 @@ export function getChannels( paginationNumber, additionalFilters, successCb, - failureCb, + _failureCb, ) { const client = algoliasearch(props.algoliaId, props.algoliaKey); const index = client.initIndex(props.algoliaIndex); @@ -90,7 +90,7 @@ export function getChannels( }, ...additionalFilters, }; - index.search(query, filters).then(function(content) { + index.search(query, filters).then(content => { const channels = content.hits; if ( retrievalID === null || @@ -98,7 +98,7 @@ export function getChannels( ) { successCb(channels, query); } else { - index.getObjects([`${retrievalID}`], function(err, content) { + index.getObjects([`${retrievalID}`], (_err, _content) => { channels.unshift(content.results[0]); successCb(channels, query); }); diff --git a/app/javascript/src/utils/__tests__/getUserData.test.js b/app/javascript/src/utils/__tests__/getUserData.test.js deleted file mode 100644 index fd9d1c78a..000000000 --- a/app/javascript/src/utils/__tests__/getUserData.test.js +++ /dev/null @@ -1,30 +0,0 @@ -import { getUserData } from '../getUserData'; - -describe('getUserData', () => { - beforeEach(() => { - jest.useFakeTimers(); - }); - - afterEach(() => { - document.body.setAttribute('data-user', null); - }); - - it('returns user data if available in document', async () => { - document.body.setAttribute('data-user', '{}'); - const data = getUserData().then(d => d); - expect(setInterval).toHaveBeenCalledTimes(1); - expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 5); - jest.runOnlyPendingTimers(); - expect(clearInterval).toHaveBeenCalledTimes(1); - await expect(data).resolves.toEqual({}); - }); - - it('return error rejects if no user data is located', async () => { - const data = getUserData().then(d => d); - expect(setInterval).toHaveBeenCalledTimes(1); - expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 5); - jest.advanceTimersByTime(200000); - expect(clearInterval).toHaveBeenCalledTimes(1); - await expect(data).rejects.toEqual("Couldn't find user data on page."); - }); -}); diff --git a/app/javascript/src/utils/getUserData.js b/app/javascript/src/utils/getUserData.js deleted file mode 100644 index c75fcb009..000000000 --- a/app/javascript/src/utils/getUserData.js +++ /dev/null @@ -1,23 +0,0 @@ -export function getUserData() { - const promise = new Promise((resolve, reject) => { - let i = 0; - const waitingOnUserData = setInterval(() => { - let userData = null; - const dataUserAttribute = document.body.getAttribute('data-user'); - if (dataUserAttribute && dataUserAttribute !== 'undefined' && dataUserAttribute !== undefined) { - userData = JSON.parse(dataUserAttribute); - } - i += 1; - if (userData) { - clearInterval(waitingOnUserData); - resolve(userData); - } else if (i === 3000) { - clearInterval(waitingOnUserData); - reject("Couldn't find user data on page."); - } - }, 5); - }); - return promise; -} - -export default 'getUserData'; diff --git a/app/javascript/src/utils/pusher.js b/app/javascript/src/utils/pusher.js index e1a0b6386..aae325a27 100644 --- a/app/javascript/src/utils/pusher.js +++ b/app/javascript/src/utils/pusher.js @@ -1,35 +1,35 @@ // import Pusher from 'pusher-js'; export default function setupPusher(key, callbackObjects) { - let pusher; - return import('pusher-js') - .then(({ }) => { - if (window.pusher) { - pusher = window.pusher - } else { + return import('pusher-js').then(_ => { + const { pusher = new Pusher(key, { authEndpoint: '/pusher/auth', auth: { headers: { - 'X-CSRF-Token': window.csrfToken - } + 'X-CSRF-Token': window.csrfToken, + }, }, cluster: 'us2', encrypted: true, - }); - window.pusher = pusher; - } + }), + } = window; + + window.pusher = pusher; + const channel = pusher.subscribe(callbackObjects.channelId.toString()); channel.bind('message-created', callbackObjects.messageCreated); channel.bind('channel-cleared', callbackObjects.channelCleared); channel.bind('user-banned', callbackObjects.redactUserMessages); channel.bind('client-livecode', callbackObjects.liveCoding); - channel.bind('client-initiatevideocall', callbackObjects.videoCallInitiated); + channel.bind( + 'client-initiatevideocall', + callbackObjects.videoCallInitiated, + ); channel.bind('client-endvideocall', callbackObjects.videoCallEnded); - + // channel.bind('pusher:subscription_succeeded', callbackObjects.channelSubscribed); channel.bind('pusher:subscription_error', callbackObjects.channelError); return channel; }); } -