-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathfindForUrl.test.ts
More file actions
120 lines (103 loc) · 3.63 KB
/
findForUrl.test.ts
File metadata and controls
120 lines (103 loc) · 3.63 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
import {describe, it, expect, vi} from 'vitest'
import * as core from '@actions/core'
import {findForUrl} from '../src/findForUrl.js'
import {AxeBuilder} from '@axe-core/playwright'
import axe from 'axe-core'
import * as pluginManager from '../src/pluginManager'
import { Plugin } from '../src/pluginManager/types.js'
import {clearCache} from '../src/scansContextProvider.js'
vi.mock('@actions/core', {spy: true})
vi.mock('playwright', () => ({
default: {
chromium: {
launch: () => ({
newContext: () => ({
newPage: () => ({
pageUrl: '',
goto: () => {},
url: () => {},
}),
close: () => {},
}),
close: () => {},
}),
},
},
}))
vi.mock('@axe-core/playwright', () => {
const AxeBuilderMock = vi.fn()
const rawFinding = {violations: []} as unknown as axe.AxeResults
AxeBuilderMock.prototype.analyze = vi.fn(() => Promise.resolve(rawFinding))
return {AxeBuilder: AxeBuilderMock}
})
let actionInput: string = ''
let loadedPlugins: Plugin[] = []
function clearAll() {
clearCache()
vi.clearAllMocks()
}
describe('findForUrl', () => {
vi.spyOn(core, 'getInput').mockImplementation(() => actionInput)
vi.spyOn(pluginManager, 'loadPlugins').mockImplementation(() => Promise.resolve(loadedPlugins))
vi.spyOn(pluginManager, 'invokePlugin')
async function axeOnlyTest() {
clearAll()
await findForUrl('test.com')
expect(AxeBuilder.prototype.analyze).toHaveBeenCalledTimes(1)
expect(pluginManager.loadPlugins).toHaveBeenCalledTimes(0)
expect(pluginManager.invokePlugin).toHaveBeenCalledTimes(0)
}
describe('when no scans list is provided', () => {
it('defaults to running only axe scan', async () => {
actionInput = ''
await axeOnlyTest()
})
})
describe('when a scans list is provided', () => {
describe('and the list _only_ includes axe', () => {
it('runs only the axe scan', async () => {
actionInput = JSON.stringify(['axe'])
await axeOnlyTest()
})
})
describe('and the list includes axe and other scans', () => {
it('runs axe and plugins', async () => {
loadedPlugins = [
{name: 'custom-scan-1', default: vi.fn()},
{name: 'custom-scan-2', default: vi.fn()},
]
actionInput = JSON.stringify(['axe', 'custom-scan-1'])
clearAll()
await findForUrl('test.com')
expect(AxeBuilder.prototype.analyze).toHaveBeenCalledTimes(1)
expect(pluginManager.loadPlugins).toHaveBeenCalledTimes(1)
expect(pluginManager.invokePlugin).toHaveBeenCalledTimes(1)
})
})
describe('and the list does not include axe', () => {
it('only runs plugins', async () => {
loadedPlugins = [
{name: 'custom-scan-1', default: vi.fn()},
{name: 'custom-scan-2', default: vi.fn()},
]
actionInput = JSON.stringify(['custom-scan-1', 'custom-scan-2'])
clearAll()
await findForUrl('test.com')
expect(AxeBuilder.prototype.analyze).toHaveBeenCalledTimes(0)
expect(pluginManager.loadPlugins).toHaveBeenCalledTimes(1)
expect(pluginManager.invokePlugin).toHaveBeenCalledTimes(2)
})
})
it('should only run scans that are included in the list', async () => {
loadedPlugins = [
{name: 'custom-scan-1', default: vi.fn()},
{name: 'custom-scan-2', default: vi.fn()},
]
actionInput = JSON.stringify(['custom-scan-1'])
clearAll()
await findForUrl('test.com')
expect(loadedPlugins[0].default).toHaveBeenCalledTimes(1)
expect(loadedPlugins[1].default).toHaveBeenCalledTimes(0)
})
})
})