|
| 1 | +import { shallow } from 'enzyme'; |
| 2 | +import 'jest'; |
| 3 | +import * as React from 'react'; |
| 4 | +import { IDiffProps } from '../../src/components/diff/Diff'; |
| 5 | +import { mergeView } from '../../src/components/diff/mergeview'; |
| 6 | +import { PlainTextDiff } from '../../src/components/diff/PlainTextDiff'; |
| 7 | +import { httpGitRequest } from '../../src/git'; |
| 8 | +import * as diffResponse from '../test-components/data/textDiffResponse.json'; |
| 9 | +import { createTestResponse } from './testutils'; |
| 10 | + |
| 11 | +jest.mock('../../src/git'); |
| 12 | +jest.mock('../../src/components/diff/mergeview'); |
| 13 | + |
| 14 | +describe('PlainTextDiff', () => { |
| 15 | + it('should render error in if API response is failed', async () => { |
| 16 | + // Given |
| 17 | + const props: IDiffProps = { |
| 18 | + path: '/path/to/File.py', |
| 19 | + topRepoPath: '/top/repo/path', |
| 20 | + diffContext: { |
| 21 | + currentRef: { specialRef: 'WORKING' }, |
| 22 | + previousRef: { gitRef: '83baee' } |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + const jsonResult = Promise.resolve({ |
| 27 | + message: 'TEST_ERROR_MESSAGE' |
| 28 | + }); |
| 29 | + |
| 30 | + (httpGitRequest as jest.Mock).mockReturnValueOnce( |
| 31 | + createTestResponse(401, jsonResult) |
| 32 | + ); |
| 33 | + |
| 34 | + // When |
| 35 | + const node = shallow<PlainTextDiff>(<PlainTextDiff {...props} />); |
| 36 | + |
| 37 | + // Then |
| 38 | + await jsonResult; |
| 39 | + |
| 40 | + node.update(); |
| 41 | + |
| 42 | + expect(httpGitRequest).toHaveBeenCalled(); |
| 43 | + expect(httpGitRequest).toBeCalledWith('/git/diffcontent', 'POST', { |
| 44 | + curr_ref: { |
| 45 | + special: 'WORKING' |
| 46 | + }, |
| 47 | + filename: '/path/to/File.py', |
| 48 | + prev_ref: { |
| 49 | + git: '83baee' |
| 50 | + }, |
| 51 | + top_repo_path: '/top/repo/path' |
| 52 | + }); |
| 53 | + expect(node.find('.jp-git-diff-error').text()).toContain( |
| 54 | + 'TEST_ERROR_MESSAGE' |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should render header and cell diff components in success case', async () => { |
| 59 | + // Given |
| 60 | + const props: IDiffProps = { |
| 61 | + path: '/path/to/File.py', |
| 62 | + topRepoPath: '/top/repo/path', |
| 63 | + diffContext: { |
| 64 | + currentRef: { specialRef: 'WORKING' }, |
| 65 | + previousRef: { gitRef: '83baee' } |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + const jsonResult = Promise.resolve(diffResponse); |
| 70 | + |
| 71 | + (httpGitRequest as jest.Mock).mockReturnValueOnce( |
| 72 | + createTestResponse(200, jsonResult) |
| 73 | + ); |
| 74 | + |
| 75 | + const mockMergeView = mergeView as jest.Mocked<typeof mergeView>; |
| 76 | + |
| 77 | + // When |
| 78 | + const node = shallow<PlainTextDiff>(<PlainTextDiff {...props} />); |
| 79 | + |
| 80 | + // Then |
| 81 | + await jsonResult; |
| 82 | + node.update(); |
| 83 | + |
| 84 | + expect(httpGitRequest).toHaveBeenCalled(); |
| 85 | + expect(httpGitRequest).toBeCalledWith('/git/diffcontent', 'POST', { |
| 86 | + curr_ref: { |
| 87 | + special: 'WORKING' |
| 88 | + }, |
| 89 | + filename: '/path/to/File.py', |
| 90 | + prev_ref: { |
| 91 | + git: '83baee' |
| 92 | + }, |
| 93 | + top_repo_path: '/top/repo/path' |
| 94 | + }); |
| 95 | + expect(node.find('.jp-git-diff-error')).toHaveLength(0); |
| 96 | + expect(mockMergeView).toBeCalled(); |
| 97 | + }); |
| 98 | +}); |
0 commit comments