|
| 1 | +const { getIndentationCharacter, writePackageJson } = require("./projectHelpers"); |
| 2 | +const fs = require("fs"); |
| 3 | + |
| 4 | +describe('projectHelpers', () => { |
| 5 | + const originalReadFileSync = fs.readFileSync; |
| 6 | + const originalWriteFileSync = fs.writeFileSync; |
| 7 | + const tab = "\t"; |
| 8 | + const multipleSpaces = " "; |
| 9 | + const twoSpaces = " "; |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + fs.readFileSync = originalReadFileSync; |
| 13 | + fs.writeFileSync = originalWriteFileSync; |
| 14 | + }); |
| 15 | + |
| 16 | + describe('getIndentationCharacter', () => { |
| 17 | + [ |
| 18 | + { |
| 19 | + testName: 'returns two spaces when file starts with two spaces', |
| 20 | + input: `{${twoSpaces}"abc": "1"${twoSpaces}}`, |
| 21 | + expectedResult: twoSpaces |
| 22 | + }, |
| 23 | + { |
| 24 | + testName: 'returns empty string when file starts without any indentation', |
| 25 | + input: `{"abc": "1"}`, |
| 26 | + expectedResult: '' |
| 27 | + }, |
| 28 | + { |
| 29 | + testName: 'returns tab when file starts with tab', |
| 30 | + input: `{${tab}"abc": "1"${tab}}`, |
| 31 | + expectedResult: tab |
| 32 | + }, |
| 33 | + { |
| 34 | + testName: 'returns two spaces when file starts with two spaces and new line before them', |
| 35 | + input: `{\n${twoSpaces}"abc": "1"\n}`, |
| 36 | + expectedResult: twoSpaces |
| 37 | + }, |
| 38 | + { |
| 39 | + testName: 'returns tab when file starts with tab and new line before them', |
| 40 | + input: `{\n${tab}"abc": "1"\n}`, |
| 41 | + expectedResult: tab |
| 42 | + }, |
| 43 | + { |
| 44 | + testName: 'returns multiple spaces when file starts with multiple spaces and new line before them', |
| 45 | + input: `{\n${multipleSpaces}"abc": "1"\n}`, |
| 46 | + expectedResult: multipleSpaces |
| 47 | + } |
| 48 | + ].forEach(({ testName, input, expectedResult }) => { |
| 49 | + it(testName, () => { |
| 50 | + expect(getIndentationCharacter(input)).toEqual(expectedResult); |
| 51 | + }); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('writePackageJson', () => { |
| 56 | + const mockFileSystemApi = () => { |
| 57 | + const data = { |
| 58 | + isWriteFileSyncCalled: false |
| 59 | + }; |
| 60 | + |
| 61 | + fs.readFileSync = (p) => { |
| 62 | + return JSON.stringify({ a: 1 }); |
| 63 | + }; |
| 64 | + |
| 65 | + fs.writeFileSync = (p, c) => { |
| 66 | + data.isWriteFileSyncCalled = true; |
| 67 | + }; |
| 68 | + |
| 69 | + return data; |
| 70 | + }; |
| 71 | + |
| 72 | + it('does not write package.json when content has not changed', () => { |
| 73 | + const data = mockFileSystemApi(); |
| 74 | + writePackageJson({ a: 1 }, "projDir"); |
| 75 | + expect(data.isWriteFileSyncCalled).toBe(false); |
| 76 | + }); |
| 77 | + |
| 78 | + it('writes content, when the new one is different from the current one', () => { |
| 79 | + const data = mockFileSystemApi(); |
| 80 | + writePackageJson({ b: 2 }, "projDir"); |
| 81 | + expect(data.isWriteFileSyncCalled).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + it('keeps indentation of the package.json when rewriting it', () => { |
| 85 | + let currentIndentSymbol = tab; |
| 86 | + fs.readFileSync = (p) => { |
| 87 | + return JSON.stringify({ a: 1 }, null, currentIndentSymbol); |
| 88 | + }; |
| 89 | + |
| 90 | + let writtenContent = null; |
| 91 | + fs.writeFileSync = (p, c) => { |
| 92 | + writtenContent = c; |
| 93 | + }; |
| 94 | + |
| 95 | + // Ensure tab indentation is persisted |
| 96 | + writePackageJson({ b: 2 }, "projDir"); |
| 97 | + expect(writtenContent).toBe(`{\n${tab}"b": 2\n}`); |
| 98 | + |
| 99 | + // Ensure spaces indentation is persisted |
| 100 | + currentIndentSymbol = multipleSpaces; |
| 101 | + writePackageJson({ b: 2 }, "projDir"); |
| 102 | + expect(writtenContent).toBe(`{\n${multipleSpaces}"b": 2\n}`); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments