Skip to content

Commit

Permalink
fix(test): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tadayosi committed Feb 23, 2023
1 parent 625b808 commit 3b9c0c4
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
7 changes: 4 additions & 3 deletions packages/hawtio/src/Hawtio.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { Hawtio } from './Hawtio'

describe('Hawtio', () => {
test('renders page', async () => {
render(<Hawtio basepath='/' />)
render(<Hawtio />)

await waitFor(() => {
const example = screen.queryByText('Hawtio')
expect(example).toBeInTheDocument()
const title = screen.queryByText('Hawtio')
expect(title).toBeInTheDocument()
})
})
})
2 changes: 1 addition & 1 deletion packages/hawtio/src/auth/user-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('UserService', () => {

test('log in as an user', async () => {
// response for fetching /user
fetchMock.mockResponse('user1')
fetchMock.mockResponse('"user1"')

const userService = new __testing__.UserService()
await expect(userService.getUsername()).resolves.toBe('user1')
Expand Down
2 changes: 1 addition & 1 deletion packages/hawtio/src/auth/user-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { eventService } from '..'
import { eventService } from '@hawtiosrc/core'
import { DEFAULT_USER, log, PATH_LOGOUT, PATH_USER } from './globals'

export interface IUserService {
Expand Down
2 changes: 1 addition & 1 deletion packages/hawtio/src/core/config-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('ConfigManager', () => {
expect(routeEnabled).toEqual(true)
})

test('filterEnabledPlugins', async () => {
test('filters enabled plugins', async () => {
// response for fetching hawtconfig.json
fetchMock.mockResponse(
JSON.stringify({
Expand Down
10 changes: 7 additions & 3 deletions packages/hawtio/src/core/config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,19 @@ class ConfigManager {

try {
const res = await fetch(HAWTCONFIG_JSON)
if (!res.ok) {
log.error('Failed to fetch', HAWTCONFIG_JSON, '-', res.status, res.statusText)
return {}
}

const config = await res.json()
log.debug(HAWTCONFIG_JSON, '=', config)
log.info('Loaded', HAWTCONFIG_JSON)
return config
} catch (err) {
log.error('Error fetching', HAWTCONFIG_JSON, '-', err)
return {}
}

return {}
}

private async applyBranding(): Promise<boolean> {
Expand Down Expand Up @@ -125,7 +129,7 @@ class ConfigManager {
async filterEnabledPlugins(plugins: Plugin[]): Promise<Plugin[]> {
const enabledPlugins: Plugin[] = []
for (const plugin of plugins) {
if (await configManager.isRouteEnabled(plugin.path)) {
if (await this.isRouteEnabled(plugin.path)) {
enabledPlugins.push(plugin)
} else {
log.debug(`Plugin "${plugin.id}" disabled by hawtconfig.json`)
Expand Down
3 changes: 2 additions & 1 deletion packages/hawtio/src/plugins/connect/jolokia-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ class JolokiaService implements IJolokiaService {
// TODO: hawtio-oauth may have already set up jQuery beforeSend?
if (!$.ajaxSettings.beforeSend) {
log.debug('Set up jQuery beforeSend')
$.ajaxSetup({ beforeSend: await this.beforeSend() })
const beforeSend = await this.beforeSend()
$.ajaxSetup({ beforeSend })
}

const options = await this.loadJolokiaOptions()
Expand Down
13 changes: 12 additions & 1 deletion packages/hawtio/src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ import '@testing-library/jest-dom/extend-expect'
import fetchMock from 'jest-fetch-mock'

fetchMock.enableMocks()

// Default mock response for every usage of fetch
fetchMock.mockResponse('{}')
fetchMock.mockResponse(req => {
console.log('Mock fetch:', req.url)
let res = '{}'
switch (req.url) {
case 'user':
res = '"public"'
break
default:
}
return Promise.resolve(res)
})

0 comments on commit 3b9c0c4

Please sign in to comment.