Skip to content

Commit 74408df

Browse files
luizhf42gustavosbarreto
authored andcommitted
fix(ui): check Welcome's hasDevices only on mount
1 parent 8cebc3a commit 74408df

File tree

3 files changed

+34
-40
lines changed

3 files changed

+34
-40
lines changed

ui/src/components/User/UserWarning.vue

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
data-test="device-chooser-component"
55
/>
66

7-
<Welcome
8-
:has-namespaces
9-
data-test="welcome-component"
10-
/>
7+
<Welcome data-test="welcome-component" />
118

129
<BillingWarning
1310
v-model="showBillingWarning"

ui/src/components/Welcome/Welcome.vue

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
</template>
8686

8787
<script setup lang="ts">
88-
import { computed, ref, watch } from "vue";
88+
import { computed, onMounted, ref, watch } from "vue";
8989
import WelcomeFirstScreen from "./WelcomeFirstScreen.vue";
9090
import WelcomeSecondScreen from "./WelcomeSecondScreen.vue";
9191
import WelcomeThirdScreen from "./WelcomeThirdScreen.vue";
@@ -106,8 +106,6 @@ interface StepConfig {
106106
action: () => void | Promise<void>;
107107
}
108108
109-
const props = defineProps<{ hasNamespaces: boolean }>();
110-
111109
const showDialog = ref(false);
112110
const authStore = useAuthStore();
113111
const namespacesStore = useNamespacesStore();
@@ -119,17 +117,14 @@ const firstPendingDevice = ref<IDevice>();
119117
const pollingTimer = ref<PollingTimer | undefined>(undefined);
120118
const hasDeviceDetected = ref(false);
121119
const tenantId = computed(() => namespacesStore.currentNamespace.tenant_id);
120+
const hasNamespaces = computed(() => namespacesStore.namespaceList.length > 0);
122121
123122
const namespaceHasBeenShown = () => (
124123
(JSON.parse(localStorage.getItem("namespacesWelcome") ?? "{}") as Record<string, boolean>)[tenantId.value] !== undefined);
125124
126-
const hasDevices = computed(() => (
127-
statsStore.stats.registered_devices !== 0
128-
|| statsStore.stats.pending_devices !== 0
129-
|| statsStore.stats.rejected_devices !== 0
130-
));
125+
const hasDevices = ref(false);
131126
132-
const shouldShowWelcome = computed(() => props.hasNamespaces && !namespaceHasBeenShown() && !hasDevices.value);
127+
const shouldShowWelcome = computed(() => hasNamespaces.value && !namespaceHasBeenShown() && !hasDevices.value);
133128
134129
watch(shouldShowWelcome, (newValue) => {
135130
if (!newValue) return;
@@ -203,5 +198,13 @@ const stepConfigs: Record<number, StepConfig> = {
203198
const currentStepConfig = computed(() => stepConfigs[currentStep.value]);
204199
const handleConfirm = async () => { await currentStepConfig.value.action(); };
205200
201+
onMounted(() => {
202+
hasDevices.value = (
203+
statsStore.stats.registered_devices > 0
204+
|| statsStore.stats.pending_devices > 0
205+
|| statsStore.stats.rejected_devices > 0
206+
);
207+
});
208+
206209
defineExpose({ currentStep, hasDeviceDetected, showDialog });
207210
</script>
Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createPinia, setActivePinia } from "pinia";
2-
import { DOMWrapper, flushPromises, mount } from "@vue/test-utils";
2+
import { DOMWrapper, flushPromises, mount, VueWrapper } from "@vue/test-utils";
33
import { createVuetify } from "vuetify";
4-
import { expect, describe, it, beforeEach, vi } from "vitest";
4+
import { expect, describe, it, beforeEach, vi, afterEach } from "vitest";
55
import Welcome from "@/components/Welcome/Welcome.vue";
66
import { SnackbarPlugin } from "@/plugins/snackbar";
77
import useNamespacesStore from "@/store/modules/namespaces";
@@ -25,6 +25,7 @@ const mockNamespace = {
2525
};
2626

2727
describe("Welcome", () => {
28+
let wrapper: VueWrapper<InstanceType<typeof Welcome>>;
2829
const vuetify = createVuetify();
2930
setActivePinia(createPinia());
3031
const namespacesStore = useNamespacesStore();
@@ -34,33 +35,37 @@ describe("Welcome", () => {
3435
vi.spyOn(Storage.prototype, "getItem").mockReturnValue("{}");
3536
vi.spyOn(Storage.prototype, "setItem");
3637
namespacesStore.currentNamespace = mockNamespace;
37-
38+
namespacesStore.namespaceList = [mockNamespace];
3839
statsStore.stats = {
3940
registered_devices: 0,
4041
pending_devices: 0,
4142
rejected_devices: 0,
4243
online_devices: 0,
4344
active_sessions: 0,
4445
};
46+
47+
wrapper = mount(Welcome, { global: { plugins: [vuetify, SnackbarPlugin] } });
4548
});
4649

47-
const mountWrapper = (hasNamespaces: boolean) => {
48-
return mount(Welcome, {
49-
global: { plugins: [vuetify, SnackbarPlugin] },
50-
props: { hasNamespaces },
51-
});
52-
};
50+
afterEach(() => { wrapper.unmount(); });
5351

54-
it("Does not render when hasNamespaces is false", async () => {
55-
mountWrapper(false);
52+
it("Enables 'Next' (confirm) button when the user sets up a device on step 2", async () => {
5653
await flushPromises();
54+
55+
wrapper.vm.currentStep = 2;
56+
wrapper.vm.hasDeviceDetected = true;
57+
wrapper.vm.showDialog = true;
58+
59+
await flushPromises();
60+
5761
const dialog = new DOMWrapper(document.body);
58-
expect(dialog.find('[data-test="welcome-window"]').exists()).toBe(false);
62+
const confirmButton = dialog.find('[data-test="confirm-btn"]');
63+
expect(confirmButton.exists()).toBe(true);
64+
expect((confirmButton.element as HTMLButtonElement).disabled).toBe(false);
5965
});
6066

6167
it("Does not render when namespace has already been shown", async () => {
6268
vi.spyOn(Storage.prototype, "getItem").mockReturnValue('{"test-tenant":true}');
63-
mountWrapper(true);
6469

6570
await flushPromises();
6671

@@ -71,27 +76,16 @@ describe("Welcome", () => {
7176
it("Does not render when namespace has devices", async () => {
7277
statsStore.stats.registered_devices = 1;
7378

74-
mountWrapper(true);
75-
7679
await flushPromises();
7780

7881
const dialog = new DOMWrapper(document.body);
7982
expect(dialog.find('[data-test="welcome-window"]').exists()).toBe(false);
8083
});
8184

82-
it("Enables 'Next' (confirm) button when the user sets up a device on step 2", async () => {
83-
const wrapper = mountWrapper(true);
84-
await flushPromises();
85-
86-
wrapper.vm.currentStep = 2;
87-
wrapper.vm.hasDeviceDetected = true;
88-
wrapper.vm.showDialog = true;
89-
85+
it("Does not render when hasNamespaces is false", async () => {
86+
namespacesStore.namespaceList = [];
9087
await flushPromises();
91-
9288
const dialog = new DOMWrapper(document.body);
93-
const confirmButton = dialog.find('[data-test="confirm-btn"]');
94-
expect(confirmButton.exists()).toBe(true);
95-
expect((confirmButton.element as HTMLButtonElement).disabled).toBe(false);
89+
expect(dialog.find('[data-test="welcome-window"]').exists()).toBe(false);
9690
});
9791
});

0 commit comments

Comments
 (0)