|
| 1 | +import React from 'React'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 4 | +import regeneratorRuntime from 'regenerator-runtime'; |
| 5 | + |
| 6 | +import Header from '../src/pages/Panel/Components/AppBar.tsx'; |
| 7 | +import Queries from '../src/pages/Panel/Components/Performance Components/Queries.tsx'; |
| 8 | +import Log from '../src/pages/Panel/Components/Performance Components/Log.tsx'; |
| 9 | + |
| 10 | +import Playground from '../src/pages/Panel/Components/Playground'; |
| 11 | +import PlaygroundHeader from '../src/pages/Panel/Components/PlaygroundHeader'; |
| 12 | + |
| 13 | +document.createRange = () => { |
| 14 | + const range = new Range(); |
| 15 | + |
| 16 | + range.getBoundingClientRect = jest.fn(); |
| 17 | + |
| 18 | + range.getClientRects = () => { |
| 19 | + return { |
| 20 | + item: () => null, |
| 21 | + length: 0, |
| 22 | + [Symbol.iterator]: jest.fn() |
| 23 | + }; |
| 24 | + }; |
| 25 | + |
| 26 | + return range; |
| 27 | +} |
| 28 | + |
| 29 | +describe('Unit testing React components', () => { |
| 30 | + describe('Side Bar', () => { |
| 31 | + test('Header is mounted', () => { |
| 32 | + const props = { |
| 33 | + count: 1, |
| 34 | + setCount: jest.fn() |
| 35 | + } |
| 36 | + render (<Header {...props}/>) |
| 37 | + const header = screen.getAllByText('Obsidian Developer Tool') |
| 38 | + expect(header.length === 1) |
| 39 | + }) |
| 40 | + }); |
| 41 | + |
| 42 | + describe('Performance Tab', () => { |
| 43 | + //check amount of queries and mutation |
| 44 | + test('Number of query log components responds to number of query messages', () => { |
| 45 | + const props = { |
| 46 | + data: ['beans', 2, 3], |
| 47 | + setGraphqlData: jest.fn(), |
| 48 | + }; |
| 49 | + const test = render(<Queries {...props} />); |
| 50 | + const logs0 = screen.getAllByText('0. Query'); |
| 51 | + expect(logs0.length).toBe(1); |
| 52 | + const logs1 = screen.getAllByText('1. Query'); |
| 53 | + expect(logs1.length).toBe(1); |
| 54 | + const logs2 = screen.getAllByText('2. Query'); |
| 55 | + expect(logs2.length).toBe(1); |
| 56 | + }); |
| 57 | + //check to see if graphql click event listener is activated |
| 58 | + test('Log functions invoked on click', () => { |
| 59 | + const props = { |
| 60 | + onClick: jest.fn(), |
| 61 | + name: 'test', |
| 62 | + }; |
| 63 | + render(<Log {...props} />); |
| 64 | + const logs = screen.getByText(props.name); |
| 65 | + userEvent.click(logs); |
| 66 | + expect(props.onClick).toHaveBeenCalledTimes(1); |
| 67 | + userEvent.click(logs); |
| 68 | + expect(props.onClick).toHaveBeenCalledTimes(2); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('Playground Tab', () => { |
| 73 | + |
| 74 | + test('Playground input field', ()=>{ |
| 75 | + render(<Playground />) |
| 76 | + userEvent.type((screen.getByText('Submit')).previousSibling, 'http://localhost:8000/graphql'); |
| 77 | + userEvent.click(screen.getByText('Submit')); |
| 78 | + expect(screen.getByRole('heading').textContent).toBe('Current Endpoint: http://localhost:8000/graphql'); |
| 79 | + }); |
| 80 | + |
| 81 | + test('Playground security test for harmful characters', ()=>{ |
| 82 | + render(<Playground />) |
| 83 | + userEvent.type((screen.getByText('Submit')).previousSibling, 'harmful characters " ) & < '); |
| 84 | + userEvent.click(screen.getByText('Submit')); |
| 85 | + expect(screen.getByRole('heading').textContent).toBe("Current Endpoint: harmful characters &amp;amp;amp;amp;amp;amp;quot; &amp;amp;amp;amp;parens; &amp;amp;amp; &lt; "); |
| 86 | + }); |
| 87 | + |
| 88 | + |
| 89 | + test('Endpoint submit button invokes function on click', () => { |
| 90 | + const props = { |
| 91 | + onEndpointChange: jest.fn() |
| 92 | + }; |
| 93 | + render(<PlaygroundHeader {...props} />) |
| 94 | + const button = screen.getByRole('button') |
| 95 | + userEvent.click(button); |
| 96 | + expect(props.onEndpointChange).toHaveBeenCalledTimes(1); |
| 97 | + userEvent.click(button); |
| 98 | + expect(props.onEndpointChange).toHaveBeenCalledTimes(2); |
| 99 | + }); |
| 100 | + |
| 101 | + test('Input value initially set to empty string with placeholder', () => { |
| 102 | + const props = { |
| 103 | + onEndpointChange: jest.fn() |
| 104 | + }; |
| 105 | + render(<PlaygroundHeader {...props} />) |
| 106 | + const input = screen.getAllByDisplayValue('') |
| 107 | + expect(input.length).toBe(1) |
| 108 | + expect(input[0].getAttribute('placeholder')).toBe('Enter GraphQL endpoint here') |
| 109 | + }); |
| 110 | + |
| 111 | + test('Input value changes with typing', () => { |
| 112 | + const props = { |
| 113 | + onEndpointChange: jest.fn() |
| 114 | + }; |
| 115 | + render(<PlaygroundHeader {...props} />) |
| 116 | + const input = screen.getByDisplayValue('') |
| 117 | + userEvent.type(input, 'hello') |
| 118 | + const newInput = screen.getAllByDisplayValue('hello') |
| 119 | + expect(newInput.length).toBe(1) |
| 120 | + }); |
| 121 | + |
| 122 | + test('Input value resets after button click', () => { |
| 123 | + const props = { |
| 124 | + onEndpointChange: jest.fn() |
| 125 | + }; |
| 126 | + render(<PlaygroundHeader {...props} />) |
| 127 | + const input = screen.getAllByDisplayValue('') |
| 128 | + expect(input.length).toBe(1) |
| 129 | + |
| 130 | + userEvent.type(screen.getByDisplayValue(''), 'hello') |
| 131 | + const testInput = screen.queryAllByDisplayValue('') |
| 132 | + expect(testInput.length).toBe(0) |
| 133 | + const newInput = screen.getAllByDisplayValue('hello') |
| 134 | + expect(newInput.length).toBe(1) |
| 135 | + const button = screen.getByRole('button') |
| 136 | + userEvent.click(button); |
| 137 | + const clickInput = screen.getAllByDisplayValue('') |
| 138 | + expect(clickInput.length).toBe(1) |
| 139 | + const oldInput = screen.queryAllByDisplayValue('hello') |
| 140 | + expect(oldInput.length).toBe(0) |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments