Tests: Improve test isolation and cleanup in vitest suites

This commit is contained in:
Ömer Duran 2025-12-14 21:34:38 +01:00
parent d4287ef81a
commit 4360fff83c
No known key found for this signature in database
GPG key ID: 2550B0D579890013
9 changed files with 106 additions and 70 deletions

View file

@ -1,4 +1,4 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import "../fixtures";
import Config from "common/config";
import StorageShim from "node-storage-shim";
@ -11,7 +11,31 @@ const createTestConfig = () => {
return new Config(new StorageShim(), values);
};
const resetThemesToDefault = () => {
themes.SetOptions([
{
text: "Default",
value: "default",
disabled: false,
},
]);
themes.Set("default", {
name: "default",
title: "Default",
colors: {},
variables: {},
});
};
describe("common/config", () => {
beforeEach(() => {
resetThemesToDefault();
});
afterEach(() => {
resetThemesToDefault();
});
it("should get all config values", () => {
const storage = new StorageShim();
const values = { siteTitle: "Foo", name: "testConfig", year: "2300" };
@ -116,42 +140,12 @@ describe("common/config", () => {
variables: {},
};
themes.SetOptions([
{
text: "Default",
value: "default",
disabled: false,
},
]);
themes.Set("default", {
name: "default",
title: "Default",
colors: {},
variables: {},
});
themes.Assign([forcedTheme]);
cfg.setTheme("default");
expect(cfg.themeName).toBe("portal-forced");
expect(cfg.theme.colors.background).toBe("#111111");
themes.Remove("portal-forced");
themes.SetOptions([
{
text: "Default",
value: "default",
disabled: false,
},
]);
themes.Set("default", {
name: "default",
title: "Default",
colors: {},
variables: {},
});
});
it("should return app edition", () => {

View file

@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { shallowMount } from "@vue/test-utils";
import { shallowMount, config as VTUConfig } from "@vue/test-utils";
import { nextTick } from "vue";
import PPhotoBatchEdit from "component/photo/batch-edit.vue";
import * as contexts from "options/contexts";
@ -16,6 +16,8 @@ vi.mock("model/thumb");
describe("component/photo/batch-edit", () => {
let wrapper;
let mockBatchInstance;
let notifySuccessSpy;
let notifyErrorSpy;
const mockSelection = ["uid1", "uid2", "uid3"];
@ -136,6 +138,9 @@ describe("component/photo/batch-edit", () => {
// Mock the Batch constructor to return our mock instance
vi.mocked(Batch).mockImplementation(() => mockBatchInstance);
notifySuccessSpy = vi.spyOn(VTUConfig.global.mocks.$notify, "success");
notifyErrorSpy = vi.spyOn(VTUConfig.global.mocks.$notify, "error");
wrapper = shallowMount(PPhotoBatchEdit, {
props: {
visible: false, // Start with false to avoid initial rendering issues
@ -146,20 +151,9 @@ describe("component/photo/batch-edit", () => {
},
global: {
mocks: {
$notify: {
success: vi.fn(),
error: vi.fn(),
},
$lightbox: {
openView: vi.fn(),
},
$event: {
subscribe: vi.fn(),
unsubscribe: vi.fn(),
},
$config: {
feature: vi.fn().mockReturnValue(true),
},
$vuetify: { display: { mdAndDown: false } },
},
stubs: {
@ -202,6 +196,7 @@ describe("component/photo/batch-edit", () => {
});
afterEach(() => {
vi.restoreAllMocks();
if (wrapper) {
wrapper.unmount();
}
@ -395,8 +390,6 @@ describe("component/photo/batch-edit", () => {
expect(ctx.allowEdit).toBe(false);
expect(ctx.allowSelect).toBe(false);
expect(ctx.context).toBe(contexts.BatchEdit);
spy.mockRestore();
});
it("should clamp invalid index to first photo", () => {
@ -407,8 +400,6 @@ describe("component/photo/batch-edit", () => {
expect(ctx.index).toBe(0);
expect(ctx.allowSelect).toBe(false);
spy.mockRestore();
});
});

View file

@ -1,8 +1,18 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import "../fixtures";
import { Album, BatchSize } from "model/album";
describe("model/album", () => {
let originalBatchSize;
beforeEach(() => {
originalBatchSize = Album.batchSize();
});
afterEach(() => {
Album.setBatchSize(originalBatchSize);
});
it("should get route view", () => {
const values = { ID: 5, Title: "Christmas 2019", Slug: "christmas-2019" };
const album = new Album(values);
@ -312,7 +322,6 @@ describe("model/album", () => {
expect(Album.batchSize()).toBe(BatchSize);
Album.setBatchSize(30);
expect(Album.batchSize()).toBe(30);
Album.setBatchSize(BatchSize);
});
it("should like album", () => {

View file

@ -1,8 +1,18 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import "../fixtures";
import { Face, BatchSize } from "model/face";
describe("model/face", () => {
let originalBatchSize;
beforeEach(() => {
originalBatchSize = Face.batchSize();
});
afterEach(() => {
Face.setBatchSize(originalBatchSize);
});
it("should get face defaults", () => {
const values = {};
const face = new Face(values);
@ -146,7 +156,6 @@ describe("model/face", () => {
expect(Face.batchSize()).toBe(BatchSize);
Face.setBatchSize(30);
expect(Face.batchSize()).toBe(30);
Face.setBatchSize(BatchSize);
});
it("should get collection resource", () => {

View file

@ -1,8 +1,18 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import "../fixtures";
import { Label, BatchSize } from "model/label";
describe("model/label", () => {
let originalBatchSize;
beforeEach(() => {
originalBatchSize = Label.batchSize();
});
afterEach(() => {
Label.setBatchSize(originalBatchSize);
});
it("should get route view", () => {
const values = { ID: 5, UID: "ABC123", Name: "Black Cat", Slug: "black-cat" };
const label = new Label(values);
@ -15,7 +25,6 @@ describe("model/label", () => {
expect(Label.batchSize()).toBe(BatchSize);
Label.setBatchSize(30);
expect(Label.batchSize()).toBe(30);
Label.setBatchSize(BatchSize);
});
it("should return classes", () => {

View file

@ -1,8 +1,18 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import "../fixtures";
import { Marker, BatchSize } from "model/marker";
describe("model/marker", () => {
let originalBatchSize;
beforeEach(() => {
originalBatchSize = Marker.batchSize();
});
afterEach(() => {
Marker.setBatchSize(originalBatchSize);
});
it("should get marker defaults", () => {
const values = { FileUID: "fghjojp" };
const marker = new Marker(values);
@ -193,7 +203,6 @@ describe("model/marker", () => {
expect(Marker.batchSize()).toBe(BatchSize);
Marker.setBatchSize(30);
expect(Marker.batchSize()).toBe(30);
Marker.setBatchSize(BatchSize);
});
it("should get collection resource", () => {

View file

@ -344,21 +344,21 @@ describe("model/photo", () => {
expect(result5).toBe("July 2012");
});
it("should test whether photo has location", () => {
it("should report hasLocation true for non-zero coordinates", () => {
const values = { ID: 5, Title: "Crazy Cat", Lat: 36.442881666666665, Lng: 28.229493333333334 };
const photo = new Photo(values);
const result = photo.hasLocation();
expect(result).toBe(true);
});
it("should test whether photo has location", () => {
it("should report hasLocation false for zero coordinates", () => {
const values = { ID: 5, Title: "Crazy Cat", Lat: 0, Lng: 0 };
const photo = new Photo(values);
const result = photo.hasLocation();
expect(result).toBe(false);
});
it("should get location", () => {
it("should get primary location label with country", () => {
const values = {
ID: 5,
Title: "Crazy Cat",
@ -372,7 +372,7 @@ describe("model/photo", () => {
expect(result).toBe("Cape Point, South Africa");
});
it("should get location", () => {
it("should get full location with state and country", () => {
const values = {
ID: 5,
Title: "Crazy Cat",
@ -389,7 +389,7 @@ describe("model/photo", () => {
expect(result).toBe("Cape Point, State, South Africa");
});
it("should get location", () => {
it("should return Unknown when country name does not match", () => {
const values = {
ID: 5,
Title: "Crazy Cat",
@ -405,14 +405,14 @@ describe("model/photo", () => {
expect(result).toBe("Unknown");
});
it("should get location", () => {
it("should return Unknown when only country name is set", () => {
const values = { ID: 5, Title: "Crazy Cat", CountryName: "Africa", PlaceCity: "Cape Town" };
const photo = new Photo(values);
const result = photo.locationInfo();
expect(result).toBe("Unknown");
});
it("should get camera", () => {
it("should get camera from model and file camera data", () => {
const values = { ID: 5, Title: "Crazy Cat", CameraModel: "EOSD10", CameraMake: "Canon" };
const photo = new Photo(values);
const result = photo.getCamera();
@ -438,7 +438,7 @@ describe("model/photo", () => {
expect(photo2.getCamera()).toBe("Canon abc");
});
it("should get camera", () => {
it("should return Unknown when camera info is missing", () => {
const values = { ID: 5, Title: "Crazy Cat" };
const photo = new Photo(values);
const result = photo.getCamera();

View file

@ -1,8 +1,18 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import "../fixtures";
import { Subject, BatchSize } from "model/subject";
describe("model/subject", () => {
let originalBatchSize;
beforeEach(() => {
originalBatchSize = Subject.batchSize();
});
afterEach(() => {
Subject.setBatchSize(originalBatchSize);
});
it("should get face defaults", () => {
const values = {};
const subject = new Subject(values);
@ -238,7 +248,6 @@ describe("model/subject", () => {
expect(Subject.batchSize()).toBe(BatchSize);
Subject.setBatchSize(30);
expect(Subject.batchSize()).toBe(30);
Subject.setBatchSize(BatchSize);
});
it("should get collection resource", () => {

View file

@ -1,4 +1,4 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import "../fixtures";
import * as options from "options/options";
import {
@ -25,6 +25,15 @@ import {
} from "options/options";
describe("options/options", () => {
let originalDefaultLocale;
beforeEach(() => {
originalDefaultLocale = options.DefaultLocale;
});
afterEach(() => {
SetDefaultLocale(originalDefaultLocale);
});
it("should get timezones", () => {
const timezones = options.TimeZones();
expect(timezones[0].ID).toBe("Local");
@ -93,13 +102,10 @@ describe("options/options", () => {
});
it("should set default locale", () => {
// Assuming DefaultLocale is exported and mutable for testing purposes
// Initial state check might depend on test execution order, so we control it here.
SetDefaultLocale("en"); // Ensure starting state
SetDefaultLocale("en");
expect(options.DefaultLocale).toBe("en");
SetDefaultLocale("de");
expect(options.DefaultLocale).toBe("de");
SetDefaultLocale("en"); // Reset for other tests
});
it("should return default when no locale is provided", () => {