/
mainAction.test.ts
107 lines (86 loc) · 2.73 KB
/
mainAction.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import * as cache from '../../src/cache'
import * as composer from '../../src/composer'
import {mainAction} from '../../src/actions'
jest.mock('../../src/cache', () => {
return {
cache: jest.fn(),
restoreFactory: jest.fn().mockReturnValue(jest.fn())
}
})
jest.mock('../../src/composer', () => {
return {
install: jest.fn()
}
})
jest.mock('../../src/utils/getCacheKeys', () => {
return {
getCacheKeys: jest.fn().mockResolvedValue({
key: 'cache-key-mock',
restoreKeys: ['cache-key-', 'cache-']
})
}
})
jest.mock('../../src/utils/getComposerCacheDir', () => {
return {
getComposerCacheDir: jest.fn().mockResolvedValue('/path/to/composer/cache')
}
})
describe('main action', () => {
const OLD_ENV = process.env
beforeEach(() => {
jest.resetModules()
process.env = {...OLD_ENV}
})
afterAll(() => {
process.env = OLD_ENV
})
test('runs', async () => {
const cacheMock = jest.spyOn(cache, 'cache')
const composerInstallMock = jest.spyOn(composer, 'install')
const mockFactory = cache.restoreFactory()
process.env['INPUT_COMPOSER-OPTIONS'] = '--ignore-platform-reqs'
process.env['INPUT_DEPENDENCY-VERSIONS'] = 'lowest'
await mainAction()
expect(cacheMock).toHaveBeenCalledTimes(1)
expect(cacheMock).toHaveBeenCalledWith(
mockFactory,
['/path/to/composer/cache'],
'cache-key-mock',
['cache-key-', 'cache-']
)
expect(composerInstallMock).toHaveBeenCalledTimes(1)
expect(composerInstallMock).toHaveBeenCalledWith(
'lowest',
'--ignore-platform-reqs',
)
})
test('runs with a different working directory', async () => {
const cacheMock = jest.spyOn(cache, 'cache')
const composerInstallMock = jest.spyOn(composer, 'install')
const mockFactory = cache.restoreFactory()
process.env['INPUT_COMPOSER-OPTIONS'] = '--ignore-platform-reqs --working-dir subdirectory'
process.env['INPUT_DEPENDENCY-VERSIONS'] = 'lowest'
await mainAction()
expect(cacheMock).toHaveBeenCalledTimes(1)
expect(cacheMock).toHaveBeenCalledWith(
mockFactory,
['/path/to/composer/cache'],
'cache-key-mock',
['cache-key-', 'cache-']
)
expect(composerInstallMock).toHaveBeenCalledTimes(1)
expect(composerInstallMock).toHaveBeenCalledWith(
'lowest',
'--ignore-platform-reqs --working-dir subdirectory',
)
})
test('sets failure state with error', async () => {
const cacheMock = jest.spyOn(cache, 'cache').mockRejectedValue({
message: 'a mocked error message'
})
const composerInstallMock = jest.spyOn(composer, 'install')
await mainAction()
expect(cacheMock).toHaveBeenCalledTimes(1)
expect(composerInstallMock).not.toHaveBeenCalled()
})
})