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

added tests for randomString and getPusher #2181

Merged
merged 7 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
21 changes: 21 additions & 0 deletions client/app/utils/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { randomString, getPusher } = require('../index');
Copy link
Member

Choose a reason for hiding this comment

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

Let's use imports here instead, I recommend just pulling in the Utils object instead of adding new exports for each function.

Suggested change
const { randomString, getPusher } = require('../index');
import Utils from '../index';

Then you can call Utils.randomString() and Utils.getPusher().


describe('Utils', () => {
describe('randomString ', () => {
it('should return random string', () => {
expect(typeof randomString()).toBe('string');
});
});

describe('getPusher', () => {
it('should return null if window.Pusher is not defined', () => {
const pusher = getPusher();
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);
});
});
});
14 changes: 5 additions & 9 deletions client/app/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ 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);
export const randomString = (): string => Math.random().toString(36).substring(2, 15)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's necessary to export these functions individually in order to be able to test them. You should be able to import the Utils object and call the functions as stated in the previous comment.

+ 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');
axios.defaults.headers.common['X-CSRF-Token'] = csrfToken;
}
};

const getPusher = (): Object | null => {
export const getPusher = (): Object | null => {
if (window.Pusher) {
const metaPusherKey = Array.from(
window.document.getElementsByTagName('meta'),
Expand All @@ -33,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));
}
Expand Down