blessing-skin-server/resources/assets/tests/js/check-updates.test.ts

70 lines
2.0 KiB
TypeScript
Raw Normal View History

2018-09-08 10:11:44 +08:00
import {
2019-03-15 11:42:41 +08:00
checkForUpdates,
checkForPluginUpdates,
} from '@/js/check-updates'
import { init } from '@/js/net'
2018-09-08 10:11:44 +08:00
test('check for BS updates', async () => {
2019-03-15 11:42:41 +08:00
window.fetch = jest.fn()
.mockResolvedValueOnce({ ok: false })
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ available: false }),
})
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ available: true, latest: '4.0.0' }),
})
2019-03-17 21:09:46 +08:00
document.body.innerHTML = '<a href="/admin/update"></a>'
2018-09-08 10:11:44 +08:00
2019-03-15 11:42:41 +08:00
await checkForUpdates()
expect(window.fetch).toBeCalledWith('/admin/update/check', init)
2019-03-17 21:09:46 +08:00
expect(document.querySelector('a')!.innerHTML).toBe('')
2018-09-08 10:11:44 +08:00
2019-03-15 11:42:41 +08:00
await checkForUpdates()
2019-03-17 21:09:46 +08:00
expect(document.querySelector('a')!.innerHTML).toBe('')
2018-09-08 10:11:44 +08:00
2019-03-15 11:42:41 +08:00
await checkForUpdates()
2019-03-17 21:09:46 +08:00
expect(document.querySelector('a')!.innerHTML).toContain('4.0.0')
2019-03-15 11:42:41 +08:00
})
2019-03-17 10:21:18 +08:00
2018-09-08 10:11:44 +08:00
test('check for plugins updates', async () => {
2019-03-15 11:42:41 +08:00
window.fetch = jest.fn()
.mockResolvedValueOnce({ ok: false })
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ available: false }),
})
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ available: true, plugins: [{}] }),
})
2019-03-17 21:09:46 +08:00
document.body.innerHTML = '<a href="/admin/plugins/market"></a>'
2018-09-08 10:11:44 +08:00
2019-03-15 11:42:41 +08:00
await checkForPluginUpdates()
expect(window.fetch).toBeCalledWith('/admin/plugins/market/check', init)
2019-03-17 21:09:46 +08:00
expect(document.querySelector('a')!.innerHTML).toBe('')
2018-09-08 10:11:44 +08:00
2019-03-15 11:42:41 +08:00
await checkForPluginUpdates()
2019-03-17 21:09:46 +08:00
expect(document.querySelector('a')!.innerHTML).toBe('')
2018-09-08 10:11:44 +08:00
2019-03-15 11:42:41 +08:00
await checkForPluginUpdates()
2019-03-17 21:09:46 +08:00
expect(document.querySelector('a')!.innerHTML).toContain('1')
})
test('do not update anything if element not found', async () => {
window.fetch = jest.fn()
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ available: true, latest: '4.0.0' }),
})
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ available: true, plugins: [{}] }),
})
await Promise.all([checkForUpdates, checkForPluginUpdates])
2019-03-15 11:42:41 +08:00
})