Skip to content

Commit

Permalink
Add more packageStore unit tests
Browse files Browse the repository at this point in the history
I'll squash this commit before merging this PR.
  • Loading branch information
djjuhasz committed Oct 3, 2024
1 parent a30763e commit 1e6bfa8
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 21 deletions.
11 changes: 6 additions & 5 deletions dashboard/src/pages/packages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import StatusBadge from "@/components/StatusBadge.vue";
import UUID from "@/components/UUID.vue";
import { useAuthStore } from "@/stores/auth";
import { useLayoutStore } from "@/stores/layout";
import { pagerPageLinks, usePackageStore } from "@/stores/package";
import { spread, usePackageStore } from "@/stores/package";
import { useAsyncState } from "@vueuse/core";
import Tooltip from "bootstrap/js/dist/tooltip";
import { onMounted } from "vue";
Expand Down Expand Up @@ -47,7 +47,8 @@ const toggleLegend = () => {
</h1>

<div class="text-muted mb-3">
Showing {{ packageStore.page.offset + 1 }} - {{ packageStore.last }} of
Showing {{ packageStore.page.offset + 1 }} -
{{ packageStore.lastResultOnPage }} of
{{ packageStore.page.total }}
</div>

Expand Down Expand Up @@ -110,7 +111,7 @@ const toggleLegend = () => {
<div v-if="packageStore.pager.total > 1">
<nav role="navigation" aria-label="Pagination navigation">
<ul class="pagination justify-content-center">
<li v-if="packageStore.pager.total > pagerPageLinks">
<li v-if="packageStore.pager.total > spread">
<a
href="#"
:class="{
Expand Down Expand Up @@ -157,7 +158,7 @@ const toggleLegend = () => {
>
</li>
<li
v-if="packageStore.pager.last < packageStore.pager.total + 1"
v-if="packageStore.pager.last < packageStore.pager.total"
aria-hidden="true"
>
<a href="#" class="page-link disabled">…</a>
Expand All @@ -175,7 +176,7 @@ const toggleLegend = () => {
Next <IconCaretRightFill />
</a>
</li>
<li v-if="packageStore.pager.total > pagerPageLinks">
<li v-if="packageStore.pager.total > spread">
<a
href="#"
:class="{
Expand Down
166 changes: 166 additions & 0 deletions dashboard/src/stores/__tests__/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,138 @@ import { usePackageStore } from "@/stores/package";
import { setActivePinia, createPinia } from "pinia";
import { expect, describe, it, beforeEach } from "vitest";
import { api } from "@/client";
import type { Pager } from "@/stores/package";

describe("usePackageStore", () => {
beforeEach(() => {
setActivePinia(createPinia());
});

it("isPending", () => {
const packageStore = usePackageStore();
const now = new Date();

expect(packageStore.isPending).toEqual(false);

packageStore.$patch((state) => {
state.current = {
createdAt: now,
id: 1,
status: api.EnduroStoredPackageStatusEnum.Pending,
};
});
expect(packageStore.isPending).toEqual(true);
});

it("isDone", () => {
const packageStore = usePackageStore();
const now = new Date();

expect(packageStore.isDone).toEqual(false);

packageStore.$patch((state) => {
state.current = {
createdAt: now,
id: 1,
status: api.EnduroStoredPackageStatusEnum.Done,
};
});
expect(packageStore.isDone).toEqual(true);
});

it("isMovable", () => {
const packageStore = usePackageStore();
const now = new Date();

expect(packageStore.isMovable).toEqual(false);

packageStore.$patch((state) => {
state.current = {
createdAt: now,
id: 1,
status: api.EnduroStoredPackageStatusEnum.Done,
};
});
expect(packageStore.isMovable).toEqual(true);
});

it("isMoving", () => {
const packageStore = usePackageStore();

expect(packageStore.isMoving).toEqual(false);

packageStore.$patch((state) => {
state.locationChanging = true;
});
expect(packageStore.isMoving).toEqual(true);
});

it("isRejected", () => {
const packageStore = usePackageStore();
const now = new Date();

expect(packageStore.isRejected).toEqual(false);

packageStore.$patch((state) => {
state.current = {
createdAt: now,
id: 1,
status: api.EnduroStoredPackageStatusEnum.Done,
};
});
expect(packageStore.isRejected).toEqual(true);
});

it("hasNextPage", () => {
const packageStore = usePackageStore();
packageStore.$patch((state) => {
state.page = { limit: 20, offset: 0, total: 20 };
});
expect(packageStore.hasNextPage).toEqual(false);

packageStore.$patch((state) => {
state.page = { limit: 20, offset: 20, total: 40 };
});
expect(packageStore.hasNextPage).toEqual(false);

packageStore.$patch((state) => {
state.page = { limit: 20, offset: 0, total: 21 };
});
expect(packageStore.hasNextPage).toEqual(true);
});

it("hasPrevPage", () => {
const packageStore = usePackageStore();
packageStore.$patch((state) => {
state.page = { limit: 20, offset: 0, total: 40 };
});
expect(packageStore.hasPrevPage).toEqual(false);

packageStore.$patch((state) => {
state.page = { limit: 20, offset: 20, total: 40 };
});
expect(packageStore.hasPrevPage).toEqual(true);
});

it("returns lastResultOnPage", () => {
const packageStore = usePackageStore();

packageStore.$patch((state) => {
state.page = { limit: 20, offset: 0, total: 40 };
});
expect(packageStore.lastResultOnPage).toEqual(20);

packageStore.$patch((state) => {
state.page = { limit: 20, offset: 0, total: 7 };
});
expect(packageStore.lastResultOnPage).toEqual(7);

packageStore.$patch((state) => {
state.page = { limit: 20, offset: 20, total: 35 };
});
expect(packageStore.lastResultOnPage).toEqual(35);
});

it("getActionById finds actions", () => {
const packageStore = usePackageStore();
const now = new Date();
Expand Down Expand Up @@ -137,4 +263,44 @@ describe("usePackageStore", () => {
});
expect(packageStore.getTaskById(2, 5)).toBeUndefined();
});

it("updates the pager", () => {
const packageStore = usePackageStore();

packageStore.$patch((state) => {
state.page = { limit: 20, offset: 60, total: 125 };
});
packageStore.updatePager;
expect(packageStore.pager).toEqual(<Pager>{
current: 4,
first: 1,
last: 7,
total: 7,
pages: [1, 2, 3, 4, 5, 6, 7],
});

packageStore.$patch((state) => {
state.page = { limit: 20, offset: 160, total: 573 };
});
packageStore.updatePager;
expect(packageStore.pager).toEqual(<Pager>{
current: 9,
first: 4,
last: 14,
total: 29,
pages: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
});

packageStore.$patch((state) => {
state.page = { limit: 20, offset: 540, total: 573 };
});
packageStore.updatePager;
expect(packageStore.pager).toEqual(<Pager>{
current: 28,
first: 19,
last: 29,
total: 29,
pages: [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
});
});
});
29 changes: 13 additions & 16 deletions dashboard/src/stores/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { mapKeys, snakeCase } from "lodash-es";

export interface Pager {
current: number;
total: number;
first: number;
last: number;
total: number;
pages: Array<number>;
}

export const pagerPageLinks = 11;
export const spread = 11;

export const usePackageStore = defineStore("package", {
state: () => ({
Expand Down Expand Up @@ -64,36 +64,33 @@ export const usePackageStore = defineStore("package", {
return this.page.offset + this.page.limit < this.page.total;
},
hasPrevPage(): boolean {
return this.page ? this.page.offset > 0 : false;
return this.page.offset > 0;
},
last(): number {
lastResultOnPage(): number {
let i = this.page.offset + this.page.limit;
if (i > this.page.total) {
i = this.page.total;
}
return i;
},
setPager(): void {
updatePager(): void {
this.pager.total = Math.ceil(this.page.total / this.page.limit);
this.pager.current = Math.floor(this.page.offset / this.page.limit) + 1;

let count =
this.pager.total <= pagerPageLinks ? this.pager.total : pagerPageLinks;
let count = this.pager.total <= spread ? this.pager.total : spread;
let first = 1;
if (this.pager.current > Math.floor(pagerPageLinks / 2) + 1) {
if (
this.pager.total - this.pager.current <
Math.floor(pagerPageLinks / 2)
) {
let halfSpread = Math.floor(spread / 2);
if (this.pager.current > halfSpread + 1) {
if (this.pager.total - this.pager.current < halfSpread) {
first = this.pager.total - count + 1;
} else {
first = this.pager.current - Math.floor(pagerPageLinks / 2);
first = this.pager.current - halfSpread;
}
}
this.pager.first = first;
this.pager.last = first + count;
this.pager.last = first + count - 1;

this.pager.pages = {} as number[];
this.pager.pages = [];
for (var i = 0; i < count; i++) {
this.pager.pages[i] = i + first;
}
Expand Down Expand Up @@ -173,7 +170,7 @@ export const usePackageStore = defineStore("package", {
});
this.packages = resp.items;
this.page = resp.page;
this.setPager;
this.updatePager;
},
async fetchPackagesDebounced(page: number) {
return this.fetchPackages(page);
Expand Down

0 comments on commit 1e6bfa8

Please sign in to comment.