|
| 1 | +import { AnalyzerBot } from './AnalyzerBot'; |
| 2 | +import { AnalyzerBotConfig } from './@interfaces/AnalyzerBotConfig'; |
| 3 | +import { |
| 4 | + file1TouchLine, |
| 5 | + file2TouchLine, |
| 6 | + mockTouchDiff, |
| 7 | + mockTouchFile, |
| 8 | + touchFileError, |
| 9 | + touchFileWarning, |
| 10 | + untouchedError, |
| 11 | + untouchedWarning, |
| 12 | +} from '../Provider/mockData'; |
| 13 | +import { MessageUtil } from './utils/message.util'; |
| 14 | + |
| 15 | +const config: AnalyzerBotConfig = { |
| 16 | + failOnWarnings: false, |
| 17 | +}; |
| 18 | +describe('AnalyzerBot', () => { |
| 19 | + const logs = [touchFileError, touchFileWarning, untouchedError, untouchedWarning]; |
| 20 | + const diff = [mockTouchDiff]; |
| 21 | + const analyzer = new AnalyzerBot(config); |
| 22 | + |
| 23 | + describe('.touchedFileLog', () => { |
| 24 | + it('should return only logs that are in touchedDiff', () => { |
| 25 | + analyzer.analyze(logs, diff); |
| 26 | + expect(analyzer.touchedFileLog).toEqual([touchFileError, touchFileWarning]); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('.comments', () => { |
| 31 | + it('should returns comments for each touched file', () => { |
| 32 | + analyzer.analyze(logs, diff); |
| 33 | + expect(analyzer.comments).toEqual([ |
| 34 | + { |
| 35 | + file: mockTouchFile, |
| 36 | + line: file1TouchLine, |
| 37 | + text: |
| 38 | + MessageUtil.createMessageWithEmoji( |
| 39 | + touchFileError.msg, |
| 40 | + touchFileError.severity, |
| 41 | + ) + ' \n', |
| 42 | + errors: 1, |
| 43 | + warnings: 0, |
| 44 | + }, |
| 45 | + { |
| 46 | + file: mockTouchFile, |
| 47 | + line: file2TouchLine, |
| 48 | + text: |
| 49 | + MessageUtil.createMessageWithEmoji( |
| 50 | + touchFileWarning.msg, |
| 51 | + touchFileWarning.severity, |
| 52 | + ) + ' \n', |
| 53 | + errors: 0, |
| 54 | + warnings: 1, |
| 55 | + }, |
| 56 | + ]); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should be empty when there is no relevant lint errors', () => { |
| 60 | + analyzer.analyze([untouchedError, untouchedWarning], diff); |
| 61 | + expect(analyzer.comments).toEqual([]); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('.nError', () => { |
| 66 | + it('should return the number of related lint errors', () => { |
| 67 | + analyzer.analyze(logs, diff); |
| 68 | + expect(analyzer.nError).toEqual(1); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('.nWarning', () => { |
| 73 | + it('should return the number of related lint warnings', () => { |
| 74 | + analyzer.analyze(logs, diff); |
| 75 | + expect(analyzer.nWarning).toEqual(1); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('.shouldGenerateOverview', () => { |
| 80 | + it('should return true when there is at least one lint error or warning', () => { |
| 81 | + analyzer.analyze(logs, diff); |
| 82 | + expect(analyzer.shouldGenerateOverview()).toEqual(true); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return false when there is no lint error or warning', () => { |
| 86 | + analyzer.analyze([untouchedError, untouchedWarning], diff); |
| 87 | + expect(analyzer.shouldGenerateOverview()).toEqual(false); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('.getOverviewMessage', () => { |
| 92 | + it('should return a proxy result from MessageUtil.generateOverviewMessage based on the number of errors and warnings', () => { |
| 93 | + analyzer.analyze(logs, diff); |
| 94 | + expect(analyzer.getOverviewMessage()).toEqual( |
| 95 | + MessageUtil.generateOverviewMessage(analyzer.nError, analyzer.nWarning), |
| 96 | + ); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('.getCommitDescription', () => { |
| 101 | + it('should return a proxy result from MessageUtil.generateCommitDescription based on the number of errors', () => { |
| 102 | + analyzer.analyze(logs, diff); |
| 103 | + expect(analyzer.getCommitDescription()).toEqual( |
| 104 | + MessageUtil.generateCommitDescription(analyzer.nError), |
| 105 | + ); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('.isSuccess', () => { |
| 110 | + describe('when failOnWarnings is false', () => { |
| 111 | + it('should return true when there is no lint error or warning', () => { |
| 112 | + analyzer.analyze([untouchedError, untouchedWarning], diff); |
| 113 | + expect(analyzer.isSuccess()).toEqual(true); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should return true when there is only lint warnings', () => { |
| 117 | + analyzer.analyze([touchFileWarning, untouchedError, untouchedWarning], diff); |
| 118 | + expect(analyzer.isSuccess()).toEqual(true); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return false when there is at least one lint error', () => { |
| 122 | + analyzer.analyze(logs, diff); |
| 123 | + expect(analyzer.isSuccess()).toEqual(false); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('when failOnWarnings is true', () => { |
| 128 | + const analyzer = new AnalyzerBot({ ...config, failOnWarnings: true }); |
| 129 | + |
| 130 | + it('should return true when there is no lint error or warning', () => { |
| 131 | + analyzer.analyze([untouchedError, untouchedWarning], diff); |
| 132 | + expect(analyzer.isSuccess()).toEqual(true); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should return false when there is a lint warning', () => { |
| 136 | + analyzer.analyze([touchFileWarning, untouchedError, untouchedWarning], diff); |
| 137 | + expect(analyzer.isSuccess()).toEqual(false); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should return false when there is a lint error', () => { |
| 141 | + analyzer.analyze(logs, diff); |
| 142 | + expect(analyzer.isSuccess()).toEqual(false); |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments