|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const noLargeSnapshots = require('../no_large_snapshots'); |
| 4 | + |
| 5 | +// was not able to use https://eslint.org/docs/developer-guide/nodejs-api#ruletester for these because there is no way to configure RuleTester to run non .js files |
| 6 | +describe('no-large-snapshots', () => { |
| 7 | + it('should return an empty object for non snapshot files', () => { |
| 8 | + const mockContext = { |
| 9 | + getFilename: () => 'mock-component.jsx', |
| 10 | + options: [], |
| 11 | + }; |
| 12 | + const result = noLargeSnapshots(mockContext); |
| 13 | + |
| 14 | + expect(result).toEqual({}); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should return an object with an ExpressionStatement function for snapshot files', () => { |
| 18 | + const mockContext = { |
| 19 | + getFilename: () => 'mock-component.jsx.snap', |
| 20 | + options: [], |
| 21 | + }; |
| 22 | + |
| 23 | + const result = noLargeSnapshots(mockContext); |
| 24 | + |
| 25 | + expect(result).toMatchObject({ |
| 26 | + ExpressionStatement: expect.any(Function), |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('ExpressionStatement function', () => { |
| 31 | + it('should report if node has more than 50 lines of code and no sizeThreshold option is passed', () => { |
| 32 | + const mockReport = jest.fn(); |
| 33 | + const mockContext = { |
| 34 | + getFilename: () => 'mock-component.jsx.snap', |
| 35 | + options: [], |
| 36 | + report: mockReport, |
| 37 | + }; |
| 38 | + const mockNode = { |
| 39 | + loc: { |
| 40 | + start: { |
| 41 | + line: 1, |
| 42 | + }, |
| 43 | + end: { |
| 44 | + line: 53, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }; |
| 48 | + noLargeSnapshots(mockContext).ExpressionStatement(mockNode); |
| 49 | + |
| 50 | + expect(mockReport).toHaveBeenCalledTimes(1); |
| 51 | + expect(mockReport.mock.calls[0]).toMatchSnapshot(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should report if node has more lines of code than number given in sizeThreshold option', () => { |
| 55 | + const mockReport = jest.fn(); |
| 56 | + const mockContext = { |
| 57 | + getFilename: () => 'mock-component.jsx.snap', |
| 58 | + options: [{ maxSize: 70 }], |
| 59 | + report: mockReport, |
| 60 | + }; |
| 61 | + const mockNode = { |
| 62 | + loc: { |
| 63 | + start: { |
| 64 | + line: 20, |
| 65 | + }, |
| 66 | + end: { |
| 67 | + line: 103, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }; |
| 71 | + noLargeSnapshots(mockContext).ExpressionStatement(mockNode); |
| 72 | + |
| 73 | + expect(mockReport).toHaveBeenCalledTimes(1); |
| 74 | + expect(mockReport.mock.calls[0]).toMatchSnapshot(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should not report if node has fewer lines of code than limit', () => { |
| 78 | + const mockReport = jest.fn(); |
| 79 | + const mockContext = { |
| 80 | + getFilename: () => 'mock-component.jsx.snap', |
| 81 | + options: [], |
| 82 | + report: mockReport, |
| 83 | + }; |
| 84 | + const mockNode = { |
| 85 | + loc: { |
| 86 | + start: { |
| 87 | + line: 1, |
| 88 | + }, |
| 89 | + end: { |
| 90 | + line: 18, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }; |
| 94 | + noLargeSnapshots(mockContext).ExpressionStatement(mockNode); |
| 95 | + |
| 96 | + expect(mockReport).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments