Skip to content

Commit 93b2c65

Browse files
luizhf42gustavosbarreto
authored andcommitted
refactor(ui): refactor admin's Stats store to setup syntax
1 parent 38847c5 commit 93b2c65

File tree

5 files changed

+49
-85
lines changed

5 files changed

+49
-85
lines changed

ui/admin/src/store/api/stats.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { adminApi } from "./../../api/http";
1+
import { adminApi } from "@admin/api/http";
22

3-
const getStats = async () => adminApi.getStats();
3+
const getAdminStats = async () => adminApi.getStats();
44

5-
export default getStats;
5+
export default getAdminStats;
Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
import { defineStore } from "pinia";
22
import { IAdminStats } from "@admin/interfaces/IStats";
3-
import getStats from "../api/stats";
3+
import getAdminStats from "../api/stats";
44

5-
export const useStatsStore = defineStore("stats", {
6-
state: () => ({
7-
stats: {} as IAdminStats,
8-
}),
5+
const useStatsStore = defineStore("stats", () => {
6+
const getStats = async () => {
7+
const { data } = await getAdminStats();
8+
return data as IAdminStats;
9+
};
910

10-
getters: {
11-
getStats: (state) => state.stats,
12-
},
13-
14-
actions: {
15-
async get() {
16-
const res = await getStats();
17-
this.stats = res.data as IAdminStats;
18-
return res;
19-
},
20-
21-
clearListState() {
22-
this.stats = {} as IAdminStats;
23-
},
24-
},
11+
return { getStats };
2512
});
2613

2714
export default useStatsStore;

ui/admin/src/views/Dashboard.vue

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121
<script setup lang="ts">
2222
import axios, { AxiosError } from "axios";
23-
import { computed, onMounted, ref } from "vue";
23+
import { onMounted, ref } from "vue";
2424
import useStatsStore from "@admin/store/modules/stats";
25+
import { IAdminStats } from "@admin/interfaces/IStats";
2526
import { StatCardItem } from "@/interfaces/IStats";
2627
import useSnackbar from "@/helpers/snackbar";
2728
import StatCard from "@/components/StatCard.vue";
@@ -30,59 +31,59 @@ const snackbar = useSnackbar();
3031
const statsStore = useStatsStore();
3132
const items = ref<StatCardItem[]>([]);
3233
const hasStatus = ref(false);
33-
const itemsStats = computed(() => statsStore.getStats);
34+
const stats = ref({} as IAdminStats);
3435
3536
onMounted(async () => {
3637
try {
37-
await statsStore.get();
38+
stats.value = await statsStore.getStats();
3839
items.value = [
3940
{
4041
title: "Registered Users",
4142
content: "Registered users",
4243
icon: "mdi-account-group",
4344
buttonLabel: "View all Users",
4445
path: "users",
45-
stat: itemsStats.value.registered_users ?? 0,
46+
stat: stats.value.registered_users ?? 0,
4647
},
4748
{
4849
title: "Registered Devices",
4950
content: "Registered devices",
5051
icon: "mdi-developer-board",
5152
buttonLabel: "View all Devices",
5253
path: "devices",
53-
stat: itemsStats.value.registered_devices ?? 0,
54+
stat: stats.value.registered_devices ?? 0,
5455
},
5556
{
5657
title: "Online Devices",
5758
content: "Devices are online and ready for connecting",
5859
icon: "mdi-developer-board",
5960
buttonLabel: "View all Devices",
6061
path: "devices",
61-
stat: itemsStats.value.online_devices ?? 0,
62+
stat: stats.value.online_devices ?? 0,
6263
},
6364
{
6465
title: "Active Sessions",
6566
content: "Active SSH Sessions opened by users",
6667
icon: "mdi-developer-board",
6768
buttonLabel: "View all Sessions",
6869
path: "sessions",
69-
stat: itemsStats.value.active_sessions ?? 0,
70+
stat: stats.value.active_sessions ?? 0,
7071
},
7172
{
7273
title: "Pending Devices",
7374
content: "Pending devices",
7475
icon: "mdi-developer-board",
7576
buttonLabel: "View all Devices",
7677
path: "devices",
77-
stat: itemsStats.value.pending_devices ?? 0,
78+
stat: stats.value.pending_devices ?? 0,
7879
},
7980
{
8081
title: "Rejected Devices",
8182
content: "Rejected devices",
8283
icon: "mdi-developer-board",
8384
buttonLabel: "View all Devices",
8485
path: "devices",
85-
stat: itemsStats.value.rejected_devices ?? 0,
86+
stat: stats.value.rejected_devices ?? 0,
8687
},
8788
];
8889
} catch (error: unknown) {
@@ -98,5 +99,5 @@ onMounted(async () => {
9899
}
99100
});
100101
101-
defineExpose({ items, itemsStats, hasStatus });
102+
defineExpose({ items, stats, hasStatus });
102103
</script>
Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
1-
import { describe, expect, it, beforeEach } from "vitest";
1+
import { describe, expect, it, vi } from "vitest";
22
import { setActivePinia, createPinia } from "pinia";
33
import useStatsStore from "@admin/store/modules/stats";
44

5-
describe("Stats Pinia Store", () => {
6-
let statsStore: ReturnType<typeof useStatsStore>;
7-
8-
const stats = {
9-
registered_devices: 2,
10-
online_devices: 1,
11-
active_sessions: 1,
12-
pending_devices: 1,
13-
registered_users: 10,
14-
rejected_devices: 0,
15-
};
16-
17-
beforeEach(() => {
18-
setActivePinia(createPinia());
19-
statsStore = useStatsStore();
20-
});
5+
const stats = {
6+
registered_devices: 2,
7+
online_devices: 1,
8+
active_sessions: 1,
9+
pending_devices: 1,
10+
registered_users: 10,
11+
rejected_devices: 0,
12+
};
2113

22-
it("returns default stats state", () => {
23-
expect(statsStore.getStats).toEqual({});
24-
});
25-
26-
it("sets stats state", () => {
27-
statsStore.stats = stats;
28-
expect(statsStore.getStats).toEqual(stats);
29-
});
14+
describe("Stats Pinia Store", () => {
15+
setActivePinia(createPinia());
16+
const statsStore = useStatsStore();
3017

31-
it("clears stats state", () => {
32-
statsStore.stats = stats;
33-
statsStore.clearListState();
34-
expect(statsStore.getStats).toEqual({});
18+
it("returns default stats state", async () => {
19+
statsStore.getStats = vi.fn().mockResolvedValue(stats);
20+
const responseData = await statsStore.getStats();
21+
expect(responseData).toEqual(stats);
3522
});
3623
});

ui/admin/tests/unit/views/Dashboard/index.spec.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { createVuetify } from "vuetify";
2-
import { mount, VueWrapper } from "@vue/test-utils";
3-
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { mount } from "@vue/test-utils";
3+
import { describe, expect, it, vi } from "vitest";
44
import { createPinia, setActivePinia } from "pinia";
55
import useStatsStore from "@admin/store/modules/stats";
66
import routes from "@admin/router";
77
import Dashboard from "@admin/views/Dashboard.vue";
88
import { SnackbarPlugin } from "@/plugins/snackbar";
99

10-
type DashboardWrapper = VueWrapper<InstanceType<typeof Dashboard>>;
11-
1210
const stats = {
1311
registered_users: 0,
1412
registered_devices: 0,
@@ -72,25 +70,16 @@ const cardsContent = [
7270
const numberOfCards = 6;
7371

7472
describe("Dashboard", () => {
75-
let wrapper: DashboardWrapper;
76-
77-
beforeEach(async () => {
78-
const pinia = createPinia();
79-
setActivePinia(pinia);
80-
81-
const statsStore = useStatsStore();
82-
statsStore.get = vi.fn().mockResolvedValue(undefined);
83-
statsStore.stats = stats;
84-
85-
const vuetify = createVuetify();
86-
87-
wrapper = mount(Dashboard, {
88-
global: {
89-
plugins: [pinia, vuetify, routes, SnackbarPlugin],
90-
},
91-
});
73+
const pinia = createPinia();
74+
setActivePinia(pinia);
75+
const statsStore = useStatsStore();
76+
statsStore.getStats = vi.fn().mockResolvedValue(stats);
77+
const vuetify = createVuetify();
9278

93-
await statsStore.get();
79+
const wrapper = mount(Dashboard, {
80+
global: {
81+
plugins: [pinia, vuetify, routes, SnackbarPlugin],
82+
},
9483
});
9584

9685
it("Is a Vue instance", () => {
@@ -103,7 +92,7 @@ describe("Dashboard", () => {
10392

10493
it("Renders the template with data", async () => {
10594
expect(wrapper.vm.items).toEqual(cardsContent);
106-
expect(wrapper.vm.itemsStats).toEqual(stats);
95+
expect(wrapper.vm.stats).toEqual(stats);
10796
expect(wrapper.vm.hasStatus).toBe(false);
10897
});
10998

0 commit comments

Comments
 (0)