-
Notifications
You must be signed in to change notification settings - Fork 218
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 environments
- Loading branch information
Showing
9 changed files
with
202 additions
and
4 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
85 changes: 85 additions & 0 deletions
85
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,85 @@ | ||
import { expect } from 'vitest'; | ||
import { fireEvent } from '@testing-library/vue'; | ||
import { render } from '@/utils/test-utils'; | ||
import EnvironmentSwitch from '@/components/environment-switch/EnvironmentSwitch.vue'; | ||
|
||
const mockReplace = vi.fn(); | ||
const mockHref = vi.fn(); | ||
|
||
Object.defineProperty(window, 'location', { | ||
value: { | ||
get href() { | ||
return mockHref(); | ||
}, | ||
replace: mockReplace, | ||
}, | ||
}); | ||
|
||
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); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
<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): string { | ||
const currentUrl = location.href; | ||
const currentEnv: ConsoleEnvironment = getCurrentEnv(); | ||
const switchedUrl = currentUrl.replace(currentEnv.url, env.url); | ||
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" | ||
v-on:click="switchToEnv(env)" | ||
> | ||
{{ 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
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