From 9d85388b3abebaf844383da76d205d3a1f87667f Mon Sep 17 00:00:00 2001 From: srpurba Date: Wed, 19 Oct 2022 03:49:03 -0400 Subject: [PATCH 1/6] added tests for randomString and getPusher --- client/app/utils/__tests__/index.test.js | 35 ++++++++++++++++++++++++ client/app/utils/index.js | 13 ++++----- 2 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 client/app/utils/__tests__/index.test.js diff --git a/client/app/utils/__tests__/index.test.js b/client/app/utils/__tests__/index.test.js new file mode 100644 index 0000000000..dca136ac0b --- /dev/null +++ b/client/app/utils/__tests__/index.test.js @@ -0,0 +1,35 @@ +import { randomString, setCsrfToken, getPusher } from '../index'; +import { JSDOM } from 'jsdom'; + + +describe('randomString, setCsrfToken, getPusher tests ', () => { + it('should return random string', () => { + expect(typeof randomString()).toBe('string'); + }); + + + it('should be equal to pusher key and cluster when getPusher is called', () => { + const dom = new JSDOM(` + + + + + + + + + `); + + + const metaPusherKey = Array.from(dom.window.document.getElementsByTagName('meta')) + .filter((item) => item.getAttribute('name') === 'pusher-key')[0]; + const metaPusherCluster = Array.from(dom.window.document.getElementsByTagName('meta')) + .filter((item) => item.getAttribute('name') === 'pusher-cluster')[0]; + + const pusher = getPusher(); + + expect(pusher.key).toBe(metaPusherKey.getAttribute('content')); + expect(pusher.config.cluster).toBe(metaPusherCluster.getAttribute('content')); + + } +} \ No newline at end of file diff --git a/client/app/utils/index.js b/client/app/utils/index.js index 57a75d18e7..fe4e17f4ad 100644 --- a/client/app/utils/index.js +++ b/client/app/utils/index.js @@ -3,12 +3,9 @@ import axios from 'axios'; import renderHTML from 'react-render-html'; import { sanitize } from 'dompurify'; -const randomString = (): string => Math.random() - .toString(36) - .substring(2, 15) - + Math.random() - .toString(36) - .substring(2, 15); +const randomString = (): string => + Math.random().toString(36).substring(2, 15) + + Math.random().toString(36).substring(2, 15); const setCsrfToken = (): void => { const tokenDom = document.querySelector('meta[name=csrf-token]'); @@ -21,10 +18,10 @@ const setCsrfToken = (): void => { const getPusher = (): Object | null => { if (window.Pusher) { const metaPusherKey = Array.from( - window.document.getElementsByTagName('meta'), + window.document.getElementsByTagName('meta') ).filter((item) => item.getAttribute('name') === 'pusher-key')[0]; const metaPusherCluster = Array.from( - window.document.getElementsByTagName('meta'), + window.document.getElementsByTagName('meta') ).filter((item) => item.getAttribute('name') === 'pusher-cluster')[0]; return new window.Pusher(metaPusherKey.getAttribute('content'), { cluster: metaPusherCluster.getAttribute('content'), From f34c0c69e8583f0c192ee0e0617e0a6eb3150520 Mon Sep 17 00:00:00 2001 From: srpurba Date: Sat, 22 Oct 2022 03:03:05 -0400 Subject: [PATCH 2/6] eslint fixes --- client/app/utils/__tests__/index.test.js | 28 +++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/client/app/utils/__tests__/index.test.js b/client/app/utils/__tests__/index.test.js index dca136ac0b..49460d79ad 100644 --- a/client/app/utils/__tests__/index.test.js +++ b/client/app/utils/__tests__/index.test.js @@ -1,13 +1,13 @@ -import { randomString, setCsrfToken, getPusher } from '../index'; import { JSDOM } from 'jsdom'; +import { randomString, setCsrfToken, getPusher } from '../index'; - -describe('randomString, setCsrfToken, getPusher tests ', () => { +describe('randomString ', () => { it('should return random string', () => { expect(typeof randomString()).toBe('string'); }); +}); - +describe('setCsrfToken', () => { it('should be equal to pusher key and cluster when getPusher is called', () => { const dom = new JSDOM(` @@ -20,16 +20,18 @@ describe('randomString, setCsrfToken, getPusher tests ', () => { `); - - const metaPusherKey = Array.from(dom.window.document.getElementsByTagName('meta')) - .filter((item) => item.getAttribute('name') === 'pusher-key')[0]; - const metaPusherCluster = Array.from(dom.window.document.getElementsByTagName('meta')) - .filter((item) => item.getAttribute('name') === 'pusher-cluster')[0]; + const metaPusherKey = Array.from( + dom.window.document.getElementsByTagName('meta'), + ).filter((item) => item.getAttribute('name') === 'pusher-key')[0]; + const metaPusherCluster = Array.from( + dom.window.document.getElementsByTagName('meta'), + ).filter((item) => item.getAttribute('name') === 'pusher-cluster')[0]; const pusher = getPusher(); expect(pusher.key).toBe(metaPusherKey.getAttribute('content')); - expect(pusher.config.cluster).toBe(metaPusherCluster.getAttribute('content')); - - } -} \ No newline at end of file + expect(pusher.config.cluster).toBe( + metaPusherCluster.getAttribute('content'), + ); + }); +}); From 15db97b4b1d7e05515a3644ae1a5b840cedad4ab Mon Sep 17 00:00:00 2001 From: srpurba Date: Sat, 22 Oct 2022 03:06:35 -0400 Subject: [PATCH 3/6] eslint modifications --- client/app/utils/__tests__/index.test.js | 56 ++++++++++++------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/client/app/utils/__tests__/index.test.js b/client/app/utils/__tests__/index.test.js index 49460d79ad..24c0732089 100644 --- a/client/app/utils/__tests__/index.test.js +++ b/client/app/utils/__tests__/index.test.js @@ -1,37 +1,39 @@ import { JSDOM } from 'jsdom'; import { randomString, setCsrfToken, getPusher } from '../index'; -describe('randomString ', () => { - it('should return random string', () => { - expect(typeof randomString()).toBe('string'); +describe('Utils', () => { + describe('randomString ', () => { + it('should return random string', () => { + expect(typeof randomString()).toBe('string'); + }); }); -}); -describe('setCsrfToken', () => { - it('should be equal to pusher key and cluster when getPusher is called', () => { - const dom = new JSDOM(` - - - - - - - - - `); + describe('setCsrfToken', () => { + it('should be equal to pusher key and cluster when getPusher is called', () => { + const dom = new JSDOM(` + + + + + + + + + `); - const metaPusherKey = Array.from( - dom.window.document.getElementsByTagName('meta'), - ).filter((item) => item.getAttribute('name') === 'pusher-key')[0]; - const metaPusherCluster = Array.from( - dom.window.document.getElementsByTagName('meta'), - ).filter((item) => item.getAttribute('name') === 'pusher-cluster')[0]; + const metaPusherKey = Array.from( + dom.window.document.getElementsByTagName('meta'), + ).filter((item) => item.getAttribute('name') === 'pusher-key')[0]; + const metaPusherCluster = Array.from( + dom.window.document.getElementsByTagName('meta'), + ).filter((item) => item.getAttribute('name') === 'pusher-cluster')[0]; - const pusher = getPusher(); + const pusher = getPusher(); - expect(pusher.key).toBe(metaPusherKey.getAttribute('content')); - expect(pusher.config.cluster).toBe( - metaPusherCluster.getAttribute('content'), - ); + expect(pusher.key).toBe(metaPusherKey.getAttribute('content')); + expect(pusher.config.cluster).toBe( + metaPusherCluster.getAttribute('content'), + ); + }); }); }); From d062391c48dc751fc590c1577b7964ed10e30f78 Mon Sep 17 00:00:00 2001 From: srpurba Date: Sat, 22 Oct 2022 03:11:22 -0400 Subject: [PATCH 4/6] index.js line break fix: eslint --- client/app/utils/index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/client/app/utils/index.js b/client/app/utils/index.js index fe4e17f4ad..c6e3275d1e 100644 --- a/client/app/utils/index.js +++ b/client/app/utils/index.js @@ -3,9 +3,8 @@ import axios from 'axios'; import renderHTML from 'react-render-html'; import { sanitize } from 'dompurify'; -const randomString = (): string => - Math.random().toString(36).substring(2, 15) + - Math.random().toString(36).substring(2, 15); +const randomString = (): string => Math.random().toString(36).substring(2, 15) + + Math.random().toString(36).substring(2, 15); const setCsrfToken = (): void => { const tokenDom = document.querySelector('meta[name=csrf-token]'); @@ -18,10 +17,10 @@ const setCsrfToken = (): void => { const getPusher = (): Object | null => { if (window.Pusher) { const metaPusherKey = Array.from( - window.document.getElementsByTagName('meta') + window.document.getElementsByTagName('meta'), ).filter((item) => item.getAttribute('name') === 'pusher-key')[0]; const metaPusherCluster = Array.from( - window.document.getElementsByTagName('meta') + window.document.getElementsByTagName('meta'), ).filter((item) => item.getAttribute('name') === 'pusher-cluster')[0]; return new window.Pusher(metaPusherKey.getAttribute('content'), { cluster: metaPusherCluster.getAttribute('content'), From eb59c78f3efd65a4988eb25ca910aed87a17c826 Mon Sep 17 00:00:00 2001 From: srpurba Date: Sat, 22 Oct 2022 03:39:38 -0400 Subject: [PATCH 5/6] eslint runs successfully --- client/app/utils/__tests__/index.test.js | 36 ++++++------------------ client/app/utils/index.js | 8 +++--- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/client/app/utils/__tests__/index.test.js b/client/app/utils/__tests__/index.test.js index 24c0732089..b82d412e82 100644 --- a/client/app/utils/__tests__/index.test.js +++ b/client/app/utils/__tests__/index.test.js @@ -1,5 +1,4 @@ -import { JSDOM } from 'jsdom'; -import { randomString, setCsrfToken, getPusher } from '../index'; +const { randomString, getPusher } = require('../index'); describe('Utils', () => { describe('randomString ', () => { @@ -8,32 +7,15 @@ describe('Utils', () => { }); }); - describe('setCsrfToken', () => { - it('should be equal to pusher key and cluster when getPusher is called', () => { - const dom = new JSDOM(` - - - - - - - - - `); - - const metaPusherKey = Array.from( - dom.window.document.getElementsByTagName('meta'), - ).filter((item) => item.getAttribute('name') === 'pusher-key')[0]; - const metaPusherCluster = Array.from( - dom.window.document.getElementsByTagName('meta'), - ).filter((item) => item.getAttribute('name') === 'pusher-cluster')[0]; - + describe('getPusher', () => { + it('should return null if window.Pusher is not defined', () => { const pusher = getPusher(); - - expect(pusher.key).toBe(metaPusherKey.getAttribute('content')); - expect(pusher.config.cluster).toBe( - metaPusherCluster.getAttribute('content'), - ); + expect(pusher).toBe(null); + }); + it('should return new window.Pusher if window.Pusher is defined', () => { + window.Pusher = jest.fn(); + const pusher = getPusher(); + expect(pusher).toBeInstanceOf(window.Pusher); }); }); }); diff --git a/client/app/utils/index.js b/client/app/utils/index.js index c6e3275d1e..1da7f73474 100644 --- a/client/app/utils/index.js +++ b/client/app/utils/index.js @@ -3,10 +3,10 @@ import axios from 'axios'; import renderHTML from 'react-render-html'; import { sanitize } from 'dompurify'; -const randomString = (): string => Math.random().toString(36).substring(2, 15) +export const randomString = (): string => Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); -const setCsrfToken = (): void => { +export const setCsrfToken = (): void => { const tokenDom = document.querySelector('meta[name=csrf-token]'); if (tokenDom) { const csrfToken = tokenDom.getAttribute('content'); @@ -14,7 +14,7 @@ const setCsrfToken = (): void => { } }; -const getPusher = (): Object | null => { +export const getPusher = (): Object | null => { if (window.Pusher) { const metaPusherKey = Array.from( window.document.getElementsByTagName('meta'), @@ -29,7 +29,7 @@ const getPusher = (): Object | null => { return null; }; -const renderContent = (content: string | any): any => { +export const renderContent = (content: string | any): any => { if (typeof content === 'string') { return renderHTML(sanitize(content)); } From b73134df6c2954cb232c3c811973c8f535cd3032 Mon Sep 17 00:00:00 2001 From: srpurba Date: Mon, 24 Oct 2022 02:54:41 -0400 Subject: [PATCH 6/6] fix:Utils test - usage of imports --- client/app/utils/__tests__/index.test.js | 9 ++++----- client/app/utils/index.js | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/client/app/utils/__tests__/index.test.js b/client/app/utils/__tests__/index.test.js index b82d412e82..34ee222b75 100644 --- a/client/app/utils/__tests__/index.test.js +++ b/client/app/utils/__tests__/index.test.js @@ -1,20 +1,19 @@ -const { randomString, getPusher } = require('../index'); +import Utils from '../index'; describe('Utils', () => { describe('randomString ', () => { it('should return random string', () => { - expect(typeof randomString()).toBe('string'); + expect(typeof Utils.randomString()).toBe('string'); }); }); describe('getPusher', () => { it('should return null if window.Pusher is not defined', () => { - const pusher = getPusher(); - expect(pusher).toBe(null); + expect(Utils.getPusher()).toBe(null); }); it('should return new window.Pusher if window.Pusher is defined', () => { window.Pusher = jest.fn(); - const pusher = getPusher(); + const pusher = Utils.getPusher(); expect(pusher).toBeInstanceOf(window.Pusher); }); }); diff --git a/client/app/utils/index.js b/client/app/utils/index.js index 1da7f73474..c6e3275d1e 100644 --- a/client/app/utils/index.js +++ b/client/app/utils/index.js @@ -3,10 +3,10 @@ import axios from 'axios'; import renderHTML from 'react-render-html'; import { sanitize } from 'dompurify'; -export const randomString = (): string => Math.random().toString(36).substring(2, 15) +const randomString = (): string => Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); -export const setCsrfToken = (): void => { +const setCsrfToken = (): void => { const tokenDom = document.querySelector('meta[name=csrf-token]'); if (tokenDom) { const csrfToken = tokenDom.getAttribute('content'); @@ -14,7 +14,7 @@ export const setCsrfToken = (): void => { } }; -export const getPusher = (): Object | null => { +const getPusher = (): Object | null => { if (window.Pusher) { const metaPusherKey = Array.from( window.document.getElementsByTagName('meta'), @@ -29,7 +29,7 @@ export const getPusher = (): Object | null => { return null; }; -export const renderContent = (content: string | any): any => { +const renderContent = (content: string | any): any => { if (typeof content === 'string') { return renderHTML(sanitize(content)); }