|
| 1 | +/** |
| 2 | + * Unit tests for basic functionality of the action's main functionality, src/main.js |
| 3 | + * Tests basic template replacement for both template-file and template-text with JSON and YAML |
| 4 | + */ |
| 5 | +const core = require('@actions/core') |
| 6 | +const main = require('../src/main') |
| 7 | + |
| 8 | +// Mock the GitHub Actions core library |
| 9 | +const getInputMock = jest.spyOn(core, 'getInput').mockImplementation() |
| 10 | +const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation() |
| 11 | + |
| 12 | +// Mock the action's main function |
| 13 | +const runMock = jest.spyOn(main, 'run') |
| 14 | + |
| 15 | +describe('Basic Functionality', () => { |
| 16 | + beforeEach(() => { |
| 17 | + jest.clearAllMocks() |
| 18 | + }) |
| 19 | + |
| 20 | + // Basic Variables - JSON - Template File |
| 21 | + it('Basic Variables - JSON - Template File', async () => { |
| 22 | + // Arrange - Mock responses for the inputs |
| 23 | + getInputMock.mockImplementation(inputName => { |
| 24 | + switch (inputName) { |
| 25 | + case 'template-file': |
| 26 | + return '__tests__/test-template.md' |
| 27 | + case 'template-vars': |
| 28 | + return JSON.stringify({ |
| 29 | + name: 'John1', |
| 30 | + user: { |
| 31 | + email: 'john1@example.com', |
| 32 | + role: 'admin' |
| 33 | + }, |
| 34 | + repositories: [ |
| 35 | + { name: 'repo1', language: 'JavaScript' }, |
| 36 | + { name: 'repo2', language: 'Python' } |
| 37 | + ], |
| 38 | + unused_value: 'unused', |
| 39 | + multiline_paragraph: ` |
| 40 | + Line 1 |
| 41 | + Line 2 |
| 42 | + `, |
| 43 | + extra_var: 'extra value' |
| 44 | + }) |
| 45 | + default: |
| 46 | + return '' |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + // Act - Load the template and replace the variables |
| 51 | + await main.run() |
| 52 | + expect(runMock).toHaveReturned() |
| 53 | + |
| 54 | + // Assert - Check output name |
| 55 | + const call = setOutputMock.mock.calls[0] |
| 56 | + const outputName = call[0] |
| 57 | + expect(outputName).toBe('updated-text') |
| 58 | + |
| 59 | + // Assert - Check inserted values |
| 60 | + const outputValue = call[1] |
| 61 | + expect(outputValue).toMatch(/Hello John1!/) |
| 62 | + expect(outputValue).toMatch(/Your email is: john1@example.com/) |
| 63 | + expect(outputValue).toMatch(/Line 1\s*Line 2/) |
| 64 | + expect(outputValue).not.toMatch(/unused/) |
| 65 | + expect(outputValue).not.toMatch(/extra value/) |
| 66 | + }) |
| 67 | + |
| 68 | + // Basic Variables - JSON - Template Text |
| 69 | + it('Basic Variables - JSON - Template Text', async () => { |
| 70 | + // Arrange - Mock responses for the inputs |
| 71 | + getInputMock.mockImplementation(inputName => { |
| 72 | + switch (inputName) { |
| 73 | + case 'template-text': |
| 74 | + return 'Hello {{ name }}' |
| 75 | + case 'template-vars': |
| 76 | + return JSON.stringify({ |
| 77 | + name: 'John1', |
| 78 | + extra_var: 'extra value' |
| 79 | + }) |
| 80 | + default: |
| 81 | + return '' |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + // Act - Load the template and replace the variables |
| 86 | + await main.run() |
| 87 | + expect(runMock).toHaveReturned() |
| 88 | + |
| 89 | + // Assert - Check output name |
| 90 | + const call = setOutputMock.mock.calls[0] |
| 91 | + const outputName = call[0] |
| 92 | + expect(outputName).toBe('updated-text') |
| 93 | + |
| 94 | + // Assert - Check inserted values |
| 95 | + const outputValue = call[1] |
| 96 | + expect(outputValue).toBe('Hello John1') |
| 97 | + expect(outputValue).not.toMatch(/extra value/) |
| 98 | + }) |
| 99 | + |
| 100 | + // Basic Variables - YAML - Template Text |
| 101 | + it('Basic Variables - YAML - Template Text', async () => { |
| 102 | + // Arrange - Mock responses for the inputs |
| 103 | + getInputMock.mockImplementation(inputName => { |
| 104 | + switch (inputName) { |
| 105 | + case 'template-text': |
| 106 | + return 'Hello {{ name }}' |
| 107 | + case 'template-vars': |
| 108 | + return ` |
| 109 | +name: John1 |
| 110 | +extra_var: extra value |
| 111 | + ` |
| 112 | + default: |
| 113 | + return '' |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + // Act - Load the template and replace the variables |
| 118 | + await main.run() |
| 119 | + expect(runMock).toHaveReturned() |
| 120 | + |
| 121 | + // Assert - Check output name |
| 122 | + const call = setOutputMock.mock.calls[0] |
| 123 | + const outputName = call[0] |
| 124 | + expect(outputName).toBe('updated-text') |
| 125 | + |
| 126 | + // Assert - Check inserted values |
| 127 | + const outputValue = call[1] |
| 128 | + expect(outputValue).toBe('Hello John1') |
| 129 | + expect(outputValue).not.toMatch(/extra value/) |
| 130 | + }) |
| 131 | + |
| 132 | + // Basic Variables - YAML - Template File |
| 133 | + it('Basic Variables - YAML - Template File', async () => { |
| 134 | + // Arrange - Mock responses for the inputs |
| 135 | + getInputMock.mockImplementation(inputName => { |
| 136 | + switch (inputName) { |
| 137 | + case 'template-file': |
| 138 | + return '__tests__/test-template.md' |
| 139 | + case 'template-vars': |
| 140 | + return ` |
| 141 | +name: John1 |
| 142 | +user: |
| 143 | + email: john1@example.com |
| 144 | + role: admin |
| 145 | +repositories: |
| 146 | + - name: repo1 |
| 147 | + language: JavaScript |
| 148 | + - name: repo2 |
| 149 | + language: Python |
| 150 | +unused_value: unused |
| 151 | +multiline_paragraph: | |
| 152 | + Line 1 |
| 153 | + Line 2 |
| 154 | +extra_var: extra value |
| 155 | + ` |
| 156 | + default: |
| 157 | + return '' |
| 158 | + } |
| 159 | + }) |
| 160 | + |
| 161 | + // Act - Load the template and replace the variables |
| 162 | + await main.run() |
| 163 | + expect(runMock).toHaveReturned() |
| 164 | + |
| 165 | + // Assert - Check output name |
| 166 | + const call = setOutputMock.mock.calls[0] |
| 167 | + const outputName = call[0] |
| 168 | + expect(outputName).toBe('updated-text') |
| 169 | + |
| 170 | + // Assert - Check inserted values |
| 171 | + const outputValue = call[1] |
| 172 | + expect(outputValue).toMatch(/Hello John1!/) |
| 173 | + expect(outputValue).toMatch(/Your email is: john1@example.com/) |
| 174 | + expect(outputValue).toMatch(/Line 1\s*Line 2/) |
| 175 | + expect(outputValue).not.toMatch(/unused/) |
| 176 | + expect(outputValue).not.toMatch(/extra value/) |
| 177 | + }) |
| 178 | +}) |
0 commit comments