-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve #1912 | Add buttons for switching between different environme…
…nts (#1919) * resolve #1912 | Add buttons for switching between different environments * resolve #1912 | Open the new tab on holding cmd or ctrl key and click
- Loading branch information
Showing
8 changed files
with
267 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
hermes-console/src/components/environment-switch/EnvironmentSwitch.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { expect } from 'vitest'; | ||
import { fireEvent } from '@testing-library/vue'; | ||
import { render } from '@/utils/test-utils'; | ||
import EnvironmentSwitch from '@/components/environment-switch/EnvironmentSwitch.vue'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
const mockReplace = vi.fn(); | ||
const mockHref = vi.fn(); | ||
const mockOpen = vi.fn(); | ||
|
||
Object.defineProperty(window, 'location', { | ||
value: { | ||
get href() { | ||
return mockHref(); | ||
}, | ||
replace: mockReplace, | ||
}, | ||
}); | ||
|
||
window.open = mockOpen; | ||
|
||
const TEST_URL_ENV_1 = | ||
'http://localhost:3000/ui/groups/pl.example.hermes/topics/pl.example.hermes.TemperatureChanged'; | ||
const TEST_URL_ENV_2 = | ||
'http://127.0.0.1:3000/ui/groups/pl.example.hermes/topics/pl.example.hermes.TemperatureChanged'; | ||
|
||
describe('EnvironmentSwitch', () => { | ||
it('should highlight the button for the selected environment', () => { | ||
// given | ||
mockHref.mockReturnValue(TEST_URL_ENV_1); | ||
|
||
// when | ||
const { getByText } = render(EnvironmentSwitch, { | ||
props: { | ||
knownEnvironments: [ | ||
{ | ||
name: 'env1', | ||
url: 'localhost:3000', | ||
}, | ||
{ | ||
name: 'env2', | ||
url: '127.0.0.1:3000', | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
// then | ||
expect(location.href).toBe( | ||
'http://localhost:3000/ui/groups/pl.example.hermes/topics/pl.example.hermes.TemperatureChanged', | ||
); | ||
expect(getByText('env1')).toBeVisible(); | ||
expect(getByText('env2')).toBeVisible(); | ||
expect(getByText('env1').closest('button')).toHaveClass('v-btn--active', { | ||
exact: false, | ||
}); | ||
expect(getByText('env2').closest('button')).not.toHaveClass( | ||
'v-btn--active', | ||
{ exact: false }, | ||
); | ||
}); | ||
|
||
it('should switch between urls without changing the rest of the path', async () => { | ||
// given | ||
mockHref.mockReturnValue(TEST_URL_ENV_1); | ||
|
||
// and | ||
const { getByText } = render(EnvironmentSwitch, { | ||
props: { | ||
knownEnvironments: [ | ||
{ | ||
name: 'env1', | ||
url: 'localhost:3000', | ||
}, | ||
{ | ||
name: 'env2', | ||
url: '127.0.0.1:3000', | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
// when | ||
await fireEvent.click(getByText('env2').closest('button')); | ||
|
||
// then | ||
expect(mockReplace).toHaveBeenCalledWith(TEST_URL_ENV_2); | ||
}); | ||
|
||
it('should open the new tab on holding ctrl and clicking', async () => { | ||
// given | ||
mockHref.mockReturnValue(TEST_URL_ENV_1); | ||
|
||
// and | ||
const { getByText } = render(EnvironmentSwitch, { | ||
props: { | ||
knownEnvironments: [ | ||
{ | ||
name: 'env1', | ||
url: 'localhost:3000', | ||
}, | ||
{ | ||
name: 'env2', | ||
url: '127.0.0.1:3000', | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
// when | ||
const user = userEvent.setup(); | ||
await user.keyboard('[ControlLeft>]'); | ||
await user.click(getByText('env2').closest('button')); | ||
|
||
// then | ||
expect(mockOpen).toHaveBeenCalledWith(TEST_URL_ENV_2, '_blank'); | ||
}); | ||
|
||
it('should open the new tab on holding cmd and clicking', async () => { | ||
// given | ||
mockHref.mockReturnValue(TEST_URL_ENV_1); | ||
|
||
// and | ||
const { getByText } = render(EnvironmentSwitch, { | ||
props: { | ||
knownEnvironments: [ | ||
{ | ||
name: 'env1', | ||
url: 'localhost:3000', | ||
}, | ||
{ | ||
name: 'env2', | ||
url: '127.0.0.1:3000', | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
// when | ||
const user = userEvent.setup(); | ||
await user.keyboard('[MetaLeft>]'); | ||
await user.click(getByText('env2').closest('button')); | ||
|
||
// then | ||
expect(mockOpen).toHaveBeenCalledWith(TEST_URL_ENV_2, '_blank'); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
hermes-console/src/components/environment-switch/EnvironmentSwitch.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<script setup lang="ts"> | ||
import { computed } from 'vue'; | ||
import type { ConsoleEnvironment } from '@/api/app-configuration'; | ||
const props = defineProps<{ | ||
knownEnvironments: ConsoleEnvironment[]; | ||
}>(); | ||
const selectedEnv = computed(() => | ||
props.knownEnvironments.indexOf(getCurrentEnv()), | ||
); | ||
function getCurrentEnv(): ConsoleEnvironment { | ||
const url = location.href; | ||
const envs = props.knownEnvironments; | ||
return envs.find( | ||
(env) => | ||
url.startsWith(env.url) || | ||
url.startsWith(`http://${env.url}`) || | ||
url.startsWith(`https://${env.url}`), | ||
); | ||
} | ||
function switchToEnv(env: ConsoleEnvironment, event): string { | ||
const currentUrl = location.href; | ||
const currentEnv: ConsoleEnvironment = getCurrentEnv(); | ||
const switchedUrl = currentUrl.replace(currentEnv.url, env.url); | ||
if (event.ctrlKey || event.metaKey) { | ||
window.open(switchedUrl, '_blank'); | ||
} else { | ||
window.location.replace(switchedUrl); | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<v-btn-toggle color="primary" rounded="0" v-model="selectedEnv" group> | ||
<v-btn | ||
v-for="env in knownEnvironments" | ||
min-width="100" | ||
v-bind:key="env.name" | ||
@click="switchToEnv(env, $event)" | ||
> | ||
{{ env.name }} | ||
</v-btn> | ||
</v-btn-toggle> | ||
</template> | ||
|
||
<style scoped lang="scss"></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 11 additions & 1 deletion
12
hermes-management/src/main/resources/console/config-local.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters