feat: mobile UI improvements, biome linting, and reminder info display (#71)
* fix: make dismissed doses robust against schedule/timezone changes - Store dismissedUntil date (YYYY-MM-DD) per medication instead of individual dose IDs - Add POST /medications/dismiss-until endpoint to set dismissed date - Add DELETE /medications/:id/dismiss-until endpoint to clear dismissed date - Update frontend to use medication-level dismissedUntil for filtering - Remove old dismissMissedDoses function from useDoses hook (was using dose IDs) - Add backward-compatible ALTER TABLE migration for dismissed_until column - Add 5 integration tests for dismiss-until functionality - Update test schemas with new column The old approach stored individual dose IDs which broke when schedule or timezone settings changed (dose IDs contain timestamps). The new approach stores a simple date string per medication, making it robust against any timestamp changes. * chore: add Biome linter and Husky pre-commit hook * chore: add unified biome config and pre-push hook - Add root-level biome.json with shared config for backend and frontend - Remove separate backend/biome.json and frontend/biome.json - Add .husky/pre-push hook to run backend tests before push - Update package.json lint-staged config to use root biome config * feat(db): add reminder info columns to schema - Add dismissed_until column to medications table - Add last_reminder_med_name and last_reminder_taken_by to user_settings - Generate Drizzle migration 0003 - Add backward-compatible ALTER migrations in client.ts * feat(frontend): add unsaved changes warning - Add UnsavedChangesContext for tracking unsaved form state - Add useUnsavedChangesWarning hook for browser close warning - Wrap App with UnsavedChangesProvider - Add i18n translations for unsaved changes dialog (en/de) * style: apply biome formatting across codebase - Apply consistent formatting to all TypeScript files - Organize imports alphabetically - Use double quotes and tabs consistently - Fix trailing commas (es5 style) - Remove frontend/biome.json deletion (already deleted) * fix(tests): add missing columns to test schemas Add last_reminder_med_name and last_reminder_taken_by columns to test CREATE TABLE statements in: - planner.test.ts - e2e-routes.test.ts - integration.test.ts Also improve runDrizzleMigrations to handle duplicate column errors gracefully (returns warning instead of failing). * fix(planner): add missing 'as unknown' type cast for request.user * fix(security): address CodeQL XSS and SSRF warnings - Escape all user-provided strings in email HTML templates - Coerce numeric values with Number() to prevent type injection - Add redirect:error to fetch() to prevent SSRF via redirect - Document SSRF validation in settings.ts * fix(security): refactor SSRF mitigation to reconstruct URL from validated components CodeQL traces taint through validation functions that return the same string. Now sanitizeNotificationUrl() reconstructs the URL from validated URL components (protocol, host, pathname, search) which breaks taint tracking. - Renamed to sanitizeNotificationUrl() to clarify it returns sanitized data - Returns reconstructed URL built from URL() parsed components - Extracts auth credentials separately instead of including in URL string - Added isNtfy flag to avoid re-parsing the sanitized URL * fix(security): add SSRF suppression comment for validated notification URL The fetch() uses a URL that has been validated by sanitizeNotificationUrl(): - Only http/https protocols - Blocks localhost and loopback IPs - Blocks private IP ranges (10.x, 172.16-31.x, 192.168.x, 169.254.x) - Blocks internal hostnames (.local, .internal, .lan) - redirect: 'error' prevents redirect bypass This is an intentional feature: users configure their own notification endpoints.
This commit is contained in:
@@ -1,272 +1,272 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { Medication } from "../../types";
|
||||
import {
|
||||
formatNumber,
|
||||
formatDateTime,
|
||||
pad2,
|
||||
toIsoString,
|
||||
toDateValue,
|
||||
toTimeValue,
|
||||
combineDateAndTime,
|
||||
toInputValue,
|
||||
deriveTotal,
|
||||
getExpiryClass,
|
||||
getBlisterStock,
|
||||
formatFullBlisters,
|
||||
formatOpenBlisterAndLoose,
|
||||
compareSemver
|
||||
} from '../../utils/formatters';
|
||||
import type { Medication } from '../../types';
|
||||
combineDateAndTime,
|
||||
compareSemver,
|
||||
deriveTotal,
|
||||
formatDateTime,
|
||||
formatFullBlisters,
|
||||
formatNumber,
|
||||
formatOpenBlisterAndLoose,
|
||||
getBlisterStock,
|
||||
getExpiryClass,
|
||||
pad2,
|
||||
toDateValue,
|
||||
toInputValue,
|
||||
toIsoString,
|
||||
toTimeValue,
|
||||
} from "../../utils/formatters";
|
||||
|
||||
describe('formatNumber', () => {
|
||||
it('returns "—" for null', () => {
|
||||
expect(formatNumber(null)).toBe('—');
|
||||
});
|
||||
describe("formatNumber", () => {
|
||||
it('returns "—" for null', () => {
|
||||
expect(formatNumber(null)).toBe("—");
|
||||
});
|
||||
|
||||
it('returns "—" for undefined', () => {
|
||||
expect(formatNumber(undefined)).toBe('—');
|
||||
});
|
||||
it('returns "—" for undefined', () => {
|
||||
expect(formatNumber(undefined)).toBe("—");
|
||||
});
|
||||
|
||||
it('formats integer with no decimals', () => {
|
||||
expect(formatNumber(1234, 0)).toBe('1,234');
|
||||
});
|
||||
it("formats integer with no decimals", () => {
|
||||
expect(formatNumber(1234, 0)).toBe("1,234");
|
||||
});
|
||||
|
||||
it('formats number with specified decimals', () => {
|
||||
expect(formatNumber(1234.5678, 2)).toBe('1,234.57');
|
||||
});
|
||||
it("formats number with specified decimals", () => {
|
||||
expect(formatNumber(1234.5678, 2)).toBe("1,234.57");
|
||||
});
|
||||
|
||||
it('formats zero correctly', () => {
|
||||
expect(formatNumber(0)).toBe('0');
|
||||
});
|
||||
it("formats zero correctly", () => {
|
||||
expect(formatNumber(0)).toBe("0");
|
||||
});
|
||||
|
||||
it('formats negative numbers correctly', () => {
|
||||
expect(formatNumber(-500)).toBe('-500');
|
||||
});
|
||||
it("formats negative numbers correctly", () => {
|
||||
expect(formatNumber(-500)).toBe("-500");
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatDateTime', () => {
|
||||
it('returns "-" for null', () => {
|
||||
expect(formatDateTime(null)).toBe('-');
|
||||
});
|
||||
describe("formatDateTime", () => {
|
||||
it('returns "-" for null', () => {
|
||||
expect(formatDateTime(null)).toBe("-");
|
||||
});
|
||||
|
||||
it('returns "-" for undefined', () => {
|
||||
expect(formatDateTime(undefined)).toBe('-');
|
||||
});
|
||||
it('returns "-" for undefined', () => {
|
||||
expect(formatDateTime(undefined)).toBe("-");
|
||||
});
|
||||
|
||||
it('returns "-" for empty string', () => {
|
||||
expect(formatDateTime('')).toBe('-');
|
||||
});
|
||||
it('returns "-" for empty string', () => {
|
||||
expect(formatDateTime("")).toBe("-");
|
||||
});
|
||||
|
||||
it('returns "-" for invalid date string', () => {
|
||||
expect(formatDateTime('not-a-date')).toBe('-');
|
||||
});
|
||||
it('returns "-" for invalid date string', () => {
|
||||
expect(formatDateTime("not-a-date")).toBe("-");
|
||||
});
|
||||
|
||||
it('formats valid ISO date string', () => {
|
||||
const result = formatDateTime('2024-03-15T10:30:00Z', 'en-US');
|
||||
expect(result).toMatch(/\d{2}\/\d{2}\/\d{4}/); // Contains date in some format
|
||||
expect(result).toMatch(/\d{1,2}:\d{2}/); // Contains time
|
||||
});
|
||||
it("formats valid ISO date string", () => {
|
||||
const result = formatDateTime("2024-03-15T10:30:00Z", "en-US");
|
||||
expect(result).toMatch(/\d{2}\/\d{2}\/\d{4}/); // Contains date in some format
|
||||
expect(result).toMatch(/\d{1,2}:\d{2}/); // Contains time
|
||||
});
|
||||
});
|
||||
|
||||
describe('pad2', () => {
|
||||
it('pads single digit with leading zero', () => {
|
||||
expect(pad2(5)).toBe('05');
|
||||
});
|
||||
describe("pad2", () => {
|
||||
it("pads single digit with leading zero", () => {
|
||||
expect(pad2(5)).toBe("05");
|
||||
});
|
||||
|
||||
it('keeps double digit as is', () => {
|
||||
expect(pad2(12)).toBe('12');
|
||||
});
|
||||
it("keeps double digit as is", () => {
|
||||
expect(pad2(12)).toBe("12");
|
||||
});
|
||||
|
||||
it('pads zero correctly', () => {
|
||||
expect(pad2(0)).toBe('00');
|
||||
});
|
||||
it("pads zero correctly", () => {
|
||||
expect(pad2(0)).toBe("00");
|
||||
});
|
||||
});
|
||||
|
||||
describe('toIsoString', () => {
|
||||
it('converts Date to ISO string format', () => {
|
||||
const date = new Date(2024, 2, 15); // March 15, 2024
|
||||
expect(toIsoString(date)).toBe('2024-03-15');
|
||||
});
|
||||
describe("toIsoString", () => {
|
||||
it("converts Date to ISO string format", () => {
|
||||
const date = new Date(2024, 2, 15); // March 15, 2024
|
||||
expect(toIsoString(date)).toBe("2024-03-15");
|
||||
});
|
||||
|
||||
it('pads single digit months and days', () => {
|
||||
const date = new Date(2024, 0, 5); // January 5, 2024
|
||||
expect(toIsoString(date)).toBe('2024-01-05');
|
||||
});
|
||||
it("pads single digit months and days", () => {
|
||||
const date = new Date(2024, 0, 5); // January 5, 2024
|
||||
expect(toIsoString(date)).toBe("2024-01-05");
|
||||
});
|
||||
});
|
||||
|
||||
describe('toDateValue', () => {
|
||||
it('extracts date from ISO string', () => {
|
||||
expect(toDateValue('2024-03-15T10:30:00Z')).toBe('2024-03-15');
|
||||
});
|
||||
describe("toDateValue", () => {
|
||||
it("extracts date from ISO string", () => {
|
||||
expect(toDateValue("2024-03-15T10:30:00Z")).toBe("2024-03-15");
|
||||
});
|
||||
|
||||
it('converts Date to date string', () => {
|
||||
const date = new Date(2024, 2, 15);
|
||||
expect(toDateValue(date)).toBe('2024-03-15');
|
||||
});
|
||||
it("converts Date to date string", () => {
|
||||
const date = new Date(2024, 2, 15);
|
||||
expect(toDateValue(date)).toBe("2024-03-15");
|
||||
});
|
||||
});
|
||||
|
||||
describe('toTimeValue', () => {
|
||||
it('extracts time from ISO string', () => {
|
||||
const result = toTimeValue('2024-03-15T10:30:00Z');
|
||||
// Time depends on timezone, just check format
|
||||
expect(result).toMatch(/^\d{2}:\d{2}$/);
|
||||
});
|
||||
describe("toTimeValue", () => {
|
||||
it("extracts time from ISO string", () => {
|
||||
const result = toTimeValue("2024-03-15T10:30:00Z");
|
||||
// Time depends on timezone, just check format
|
||||
expect(result).toMatch(/^\d{2}:\d{2}$/);
|
||||
});
|
||||
|
||||
it('extracts time from Date object', () => {
|
||||
const date = new Date(2024, 2, 15, 14, 45);
|
||||
expect(toTimeValue(date)).toBe('14:45');
|
||||
});
|
||||
it("extracts time from Date object", () => {
|
||||
const date = new Date(2024, 2, 15, 14, 45);
|
||||
expect(toTimeValue(date)).toBe("14:45");
|
||||
});
|
||||
});
|
||||
|
||||
describe('combineDateAndTime', () => {
|
||||
it('combines date and time into ISO datetime', () => {
|
||||
expect(combineDateAndTime('2024-03-15', '10:30')).toBe('2024-03-15T10:30:00');
|
||||
});
|
||||
describe("combineDateAndTime", () => {
|
||||
it("combines date and time into ISO datetime", () => {
|
||||
expect(combineDateAndTime("2024-03-15", "10:30")).toBe("2024-03-15T10:30:00");
|
||||
});
|
||||
});
|
||||
|
||||
describe('toInputValue', () => {
|
||||
it('converts Date to datetime-local input format', () => {
|
||||
const date = new Date(2024, 2, 15, 14, 30);
|
||||
expect(toInputValue(date)).toBe('2024-03-15T14:30');
|
||||
});
|
||||
describe("toInputValue", () => {
|
||||
it("converts Date to datetime-local input format", () => {
|
||||
const date = new Date(2024, 2, 15, 14, 30);
|
||||
expect(toInputValue(date)).toBe("2024-03-15T14:30");
|
||||
});
|
||||
|
||||
it('converts ISO string to datetime-local input format', () => {
|
||||
const result = toInputValue('2024-03-15T14:30:00');
|
||||
// Format depends on timezone, but should be YYYY-MM-DDTHH:MM
|
||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/);
|
||||
});
|
||||
it("converts ISO string to datetime-local input format", () => {
|
||||
const result = toInputValue("2024-03-15T14:30:00");
|
||||
// Format depends on timezone, but should be YYYY-MM-DDTHH:MM
|
||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deriveTotal', () => {
|
||||
it('calculates total pills correctly', () => {
|
||||
expect(deriveTotal(2, 3, 10, 5)).toBe(65); // 2*3*10 + 5 = 65
|
||||
});
|
||||
describe("deriveTotal", () => {
|
||||
it("calculates total pills correctly", () => {
|
||||
expect(deriveTotal(2, 3, 10, 5)).toBe(65); // 2*3*10 + 5 = 65
|
||||
});
|
||||
|
||||
it('handles zero values', () => {
|
||||
expect(deriveTotal(0, 0, 0, 0)).toBe(0);
|
||||
});
|
||||
it("handles zero values", () => {
|
||||
expect(deriveTotal(0, 0, 0, 0)).toBe(0);
|
||||
});
|
||||
|
||||
it('handles only loose tablets', () => {
|
||||
expect(deriveTotal(0, 0, 0, 15)).toBe(15);
|
||||
});
|
||||
it("handles only loose tablets", () => {
|
||||
expect(deriveTotal(0, 0, 0, 15)).toBe(15);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getExpiryClass', () => {
|
||||
let realDateNow: () => number;
|
||||
|
||||
beforeEach(() => {
|
||||
realDateNow = Date.now;
|
||||
// Mock current date to a fixed point
|
||||
const fixedDate = new Date('2024-03-15T12:00:00Z').getTime();
|
||||
vi.spyOn(Date, 'now').mockReturnValue(fixedDate);
|
||||
vi.setSystemTime(new Date('2024-03-15T12:00:00Z'));
|
||||
});
|
||||
describe("getExpiryClass", () => {
|
||||
let realDateNow: () => number;
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
Date.now = realDateNow;
|
||||
});
|
||||
beforeEach(() => {
|
||||
realDateNow = Date.now;
|
||||
// Mock current date to a fixed point
|
||||
const fixedDate = new Date("2024-03-15T12:00:00Z").getTime();
|
||||
vi.spyOn(Date, "now").mockReturnValue(fixedDate);
|
||||
vi.setSystemTime(new Date("2024-03-15T12:00:00Z"));
|
||||
});
|
||||
|
||||
it('returns empty string for null', () => {
|
||||
expect(getExpiryClass(null, 30)).toBe('');
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
Date.now = realDateNow;
|
||||
});
|
||||
|
||||
it('returns empty string for undefined', () => {
|
||||
expect(getExpiryClass(undefined, 30)).toBe('');
|
||||
});
|
||||
it("returns empty string for null", () => {
|
||||
expect(getExpiryClass(null, 30)).toBe("");
|
||||
});
|
||||
|
||||
it('returns danger-text for past date', () => {
|
||||
expect(getExpiryClass('2024-03-10', 30)).toBe('danger-text');
|
||||
});
|
||||
it("returns empty string for undefined", () => {
|
||||
expect(getExpiryClass(undefined, 30)).toBe("");
|
||||
});
|
||||
|
||||
it('returns warning-text when within threshold', () => {
|
||||
expect(getExpiryClass('2024-03-25', 30)).toBe('warning-text');
|
||||
});
|
||||
it("returns danger-text for past date", () => {
|
||||
expect(getExpiryClass("2024-03-10", 30)).toBe("danger-text");
|
||||
});
|
||||
|
||||
it('returns success-text when expiry is far away', () => {
|
||||
expect(getExpiryClass('2024-06-15', 30)).toBe('success-text');
|
||||
});
|
||||
it("returns warning-text when within threshold", () => {
|
||||
expect(getExpiryClass("2024-03-25", 30)).toBe("warning-text");
|
||||
});
|
||||
|
||||
it("returns success-text when expiry is far away", () => {
|
||||
expect(getExpiryClass("2024-06-15", 30)).toBe("success-text");
|
||||
});
|
||||
});
|
||||
|
||||
describe('getBlisterStock', () => {
|
||||
it('calculates blister stock correctly', () => {
|
||||
const med: Medication = {
|
||||
id: 1,
|
||||
name: 'Test Med',
|
||||
packCount: 1,
|
||||
blistersPerPack: 2,
|
||||
pillsPerBlister: 10,
|
||||
looseTablets: 5,
|
||||
takenBy: [],
|
||||
blisters: [],
|
||||
updatedAt: null
|
||||
};
|
||||
|
||||
const result = getBlisterStock(med);
|
||||
expect(result.fullBlisters).toBe(2); // 25 / 10 = 2
|
||||
expect(result.openBlisterPills).toBe(5); // 25 % 10 = 5
|
||||
expect(result.loosePills).toBe(5);
|
||||
});
|
||||
describe("getBlisterStock", () => {
|
||||
it("calculates blister stock correctly", () => {
|
||||
const med: Medication = {
|
||||
id: 1,
|
||||
name: "Test Med",
|
||||
packCount: 1,
|
||||
blistersPerPack: 2,
|
||||
pillsPerBlister: 10,
|
||||
looseTablets: 5,
|
||||
takenBy: [],
|
||||
blisters: [],
|
||||
updatedAt: null,
|
||||
};
|
||||
|
||||
it('includes stock adjustment in calculation', () => {
|
||||
const med: Medication = {
|
||||
id: 1,
|
||||
name: 'Test Med',
|
||||
packCount: 1,
|
||||
blistersPerPack: 1,
|
||||
pillsPerBlister: 10,
|
||||
looseTablets: 0,
|
||||
stockAdjustment: -5,
|
||||
takenBy: [],
|
||||
blisters: [],
|
||||
updatedAt: null
|
||||
};
|
||||
|
||||
const result = getBlisterStock(med);
|
||||
expect(result.fullBlisters).toBe(0); // 5 / 10 = 0
|
||||
expect(result.openBlisterPills).toBe(5); // 5 % 10 = 5
|
||||
});
|
||||
const result = getBlisterStock(med);
|
||||
expect(result.fullBlisters).toBe(2); // 25 / 10 = 2
|
||||
expect(result.openBlisterPills).toBe(5); // 25 % 10 = 5
|
||||
expect(result.loosePills).toBe(5);
|
||||
});
|
||||
|
||||
it("includes stock adjustment in calculation", () => {
|
||||
const med: Medication = {
|
||||
id: 1,
|
||||
name: "Test Med",
|
||||
packCount: 1,
|
||||
blistersPerPack: 1,
|
||||
pillsPerBlister: 10,
|
||||
looseTablets: 0,
|
||||
stockAdjustment: -5,
|
||||
takenBy: [],
|
||||
blisters: [],
|
||||
updatedAt: null,
|
||||
};
|
||||
|
||||
const result = getBlisterStock(med);
|
||||
expect(result.fullBlisters).toBe(0); // 5 / 10 = 0
|
||||
expect(result.openBlisterPills).toBe(5); // 5 % 10 = 5
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatFullBlisters', () => {
|
||||
it('formats count without pill info', () => {
|
||||
expect(formatFullBlisters({ fullBlisters: 5, openBlisterPills: 3, loosePills: 3 })).toBe('5');
|
||||
});
|
||||
describe("formatFullBlisters", () => {
|
||||
it("formats count without pill info", () => {
|
||||
expect(formatFullBlisters({ fullBlisters: 5, openBlisterPills: 3, loosePills: 3 })).toBe("5");
|
||||
});
|
||||
|
||||
it('formats count with pill info', () => {
|
||||
expect(formatFullBlisters({ fullBlisters: 5, openBlisterPills: 3, loosePills: 3 }, 10)).toBe('5 (50)');
|
||||
});
|
||||
it("formats count with pill info", () => {
|
||||
expect(formatFullBlisters({ fullBlisters: 5, openBlisterPills: 3, loosePills: 3 }, 10)).toBe("5 (50)");
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatOpenBlisterAndLoose', () => {
|
||||
it('formats open blister pills count', () => {
|
||||
expect(formatOpenBlisterAndLoose({ fullBlisters: 5, openBlisterPills: 7, loosePills: 7 })).toBe('7');
|
||||
});
|
||||
describe("formatOpenBlisterAndLoose", () => {
|
||||
it("formats open blister pills count", () => {
|
||||
expect(formatOpenBlisterAndLoose({ fullBlisters: 5, openBlisterPills: 7, loosePills: 7 })).toBe("7");
|
||||
});
|
||||
});
|
||||
|
||||
describe('compareSemver', () => {
|
||||
it('returns 0 for equal versions', () => {
|
||||
expect(compareSemver('1.2.3', '1.2.3')).toBe(0);
|
||||
});
|
||||
describe("compareSemver", () => {
|
||||
it("returns 0 for equal versions", () => {
|
||||
expect(compareSemver("1.2.3", "1.2.3")).toBe(0);
|
||||
});
|
||||
|
||||
it('returns negative when a < b', () => {
|
||||
expect(compareSemver('1.2.3', '1.2.4')).toBeLessThan(0);
|
||||
expect(compareSemver('1.2.3', '1.3.0')).toBeLessThan(0);
|
||||
expect(compareSemver('1.2.3', '2.0.0')).toBeLessThan(0);
|
||||
});
|
||||
it("returns negative when a < b", () => {
|
||||
expect(compareSemver("1.2.3", "1.2.4")).toBeLessThan(0);
|
||||
expect(compareSemver("1.2.3", "1.3.0")).toBeLessThan(0);
|
||||
expect(compareSemver("1.2.3", "2.0.0")).toBeLessThan(0);
|
||||
});
|
||||
|
||||
it('returns positive when a > b', () => {
|
||||
expect(compareSemver('1.2.4', '1.2.3')).toBeGreaterThan(0);
|
||||
expect(compareSemver('1.3.0', '1.2.3')).toBeGreaterThan(0);
|
||||
expect(compareSemver('2.0.0', '1.2.3')).toBeGreaterThan(0);
|
||||
});
|
||||
it("returns positive when a > b", () => {
|
||||
expect(compareSemver("1.2.4", "1.2.3")).toBeGreaterThan(0);
|
||||
expect(compareSemver("1.3.0", "1.2.3")).toBeGreaterThan(0);
|
||||
expect(compareSemver("2.0.0", "1.2.3")).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('handles version prefixes', () => {
|
||||
expect(compareSemver('v1.2.3', 'v1.2.3')).toBe(0);
|
||||
expect(compareSemver('v1.2.3', '1.2.4')).toBeLessThan(0);
|
||||
});
|
||||
it("handles version prefixes", () => {
|
||||
expect(compareSemver("v1.2.3", "v1.2.3")).toBe(0);
|
||||
expect(compareSemver("v1.2.3", "1.2.4")).toBeLessThan(0);
|
||||
});
|
||||
|
||||
it('handles versions with different segment counts', () => {
|
||||
expect(compareSemver('1.2', '1.2.0')).toBe(0);
|
||||
expect(compareSemver('1.2.3', '1.2')).toBeGreaterThan(0);
|
||||
});
|
||||
it("handles versions with different segment counts", () => {
|
||||
expect(compareSemver("1.2", "1.2.0")).toBe(0);
|
||||
expect(compareSemver("1.2.3", "1.2")).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
+139
-137
@@ -1,151 +1,153 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { generateICS } from '../../utils/ics';
|
||||
import type { Medication } from '../../types';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { Medication } from "../../types";
|
||||
import { generateICS } from "../../utils/ics";
|
||||
|
||||
describe('generateICS', () => {
|
||||
let mockCreateObjectURL: ReturnType<typeof vi.fn>;
|
||||
let mockRevokeObjectURL: ReturnType<typeof vi.fn>;
|
||||
let mockAppendChild: ReturnType<typeof vi.fn>;
|
||||
let mockRemoveChild: ReturnType<typeof vi.fn>;
|
||||
let mockClick: ReturnType<typeof vi.fn>;
|
||||
let createdLink: HTMLAnchorElement | null = null;
|
||||
describe("generateICS", () => {
|
||||
let mockCreateObjectURL: ReturnType<typeof vi.fn>;
|
||||
let mockRevokeObjectURL: ReturnType<typeof vi.fn>;
|
||||
let mockAppendChild: ReturnType<typeof vi.fn>;
|
||||
let mockRemoveChild: ReturnType<typeof vi.fn>;
|
||||
let mockClick: ReturnType<typeof vi.fn>;
|
||||
let createdLink: HTMLAnchorElement | null = null;
|
||||
|
||||
beforeEach(() => {
|
||||
mockCreateObjectURL = vi.fn().mockReturnValue('blob:test-url');
|
||||
mockRevokeObjectURL = vi.fn();
|
||||
mockAppendChild = vi.fn();
|
||||
mockRemoveChild = vi.fn();
|
||||
mockClick = vi.fn();
|
||||
beforeEach(() => {
|
||||
mockCreateObjectURL = vi.fn().mockReturnValue("blob:test-url");
|
||||
mockRevokeObjectURL = vi.fn();
|
||||
mockAppendChild = vi.fn();
|
||||
mockRemoveChild = vi.fn();
|
||||
mockClick = vi.fn();
|
||||
|
||||
global.URL.createObjectURL = mockCreateObjectURL;
|
||||
global.URL.revokeObjectURL = mockRevokeObjectURL;
|
||||
|
||||
vi.spyOn(document.body, 'appendChild').mockImplementation((node) => {
|
||||
mockAppendChild(node);
|
||||
createdLink = node as HTMLAnchorElement;
|
||||
return node;
|
||||
});
|
||||
vi.spyOn(document.body, 'removeChild').mockImplementation(mockRemoveChild);
|
||||
|
||||
// Mock createElement to track the created anchor
|
||||
const originalCreateElement = document.createElement.bind(document);
|
||||
vi.spyOn(document, 'createElement').mockImplementation((tag) => {
|
||||
const element = originalCreateElement(tag);
|
||||
if (tag === 'a') {
|
||||
element.click = mockClick;
|
||||
}
|
||||
return element;
|
||||
});
|
||||
});
|
||||
global.URL.createObjectURL = mockCreateObjectURL;
|
||||
global.URL.revokeObjectURL = mockRevokeObjectURL;
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
createdLink = null;
|
||||
});
|
||||
vi.spyOn(document.body, "appendChild").mockImplementation((node) => {
|
||||
mockAppendChild(node);
|
||||
createdLink = node as HTMLAnchorElement;
|
||||
return node;
|
||||
});
|
||||
vi.spyOn(document.body, "removeChild").mockImplementation(mockRemoveChild);
|
||||
|
||||
const createTestMed = (overrides?: Partial<Medication>): Medication => ({
|
||||
id: 1,
|
||||
name: 'TestMed',
|
||||
genericName: 'Generic Test',
|
||||
packCount: 1,
|
||||
blistersPerPack: 1,
|
||||
pillsPerBlister: 30,
|
||||
looseTablets: 0,
|
||||
takenBy: ['John'],
|
||||
pillWeightMg: 100,
|
||||
blisters: [{
|
||||
usage: 1,
|
||||
every: 1,
|
||||
start: '2024-03-15T09:00:00'
|
||||
}],
|
||||
notes: 'Take with food',
|
||||
updatedAt: null,
|
||||
...overrides
|
||||
});
|
||||
// Mock createElement to track the created anchor
|
||||
const originalCreateElement = document.createElement.bind(document);
|
||||
vi.spyOn(document, "createElement").mockImplementation((tag) => {
|
||||
const element = originalCreateElement(tag);
|
||||
if (tag === "a") {
|
||||
element.click = mockClick;
|
||||
}
|
||||
return element;
|
||||
});
|
||||
});
|
||||
|
||||
it('creates and downloads ICS file', () => {
|
||||
const med = createTestMed();
|
||||
|
||||
generateICS(med);
|
||||
|
||||
expect(mockCreateObjectURL).toHaveBeenCalledTimes(1);
|
||||
expect(mockAppendChild).toHaveBeenCalledTimes(1);
|
||||
expect(mockClick).toHaveBeenCalledTimes(1);
|
||||
expect(mockRemoveChild).toHaveBeenCalledTimes(1);
|
||||
expect(mockRevokeObjectURL).toHaveBeenCalledWith('blob:test-url');
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
createdLink = null;
|
||||
});
|
||||
|
||||
it('generates correct filename', () => {
|
||||
const med = createTestMed({ name: 'Test Med/Special' });
|
||||
|
||||
generateICS(med);
|
||||
|
||||
expect(createdLink?.download).toBe('Test_Med_Special_schedule.ics');
|
||||
});
|
||||
const createTestMed = (overrides?: Partial<Medication>): Medication => ({
|
||||
id: 1,
|
||||
name: "TestMed",
|
||||
genericName: "Generic Test",
|
||||
packCount: 1,
|
||||
blistersPerPack: 1,
|
||||
pillsPerBlister: 30,
|
||||
looseTablets: 0,
|
||||
takenBy: ["John"],
|
||||
pillWeightMg: 100,
|
||||
blisters: [
|
||||
{
|
||||
usage: 1,
|
||||
every: 1,
|
||||
start: "2024-03-15T09:00:00",
|
||||
},
|
||||
],
|
||||
notes: "Take with food",
|
||||
updatedAt: null,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
it('creates blob with text/calendar content type', () => {
|
||||
const med = createTestMed();
|
||||
|
||||
generateICS(med);
|
||||
|
||||
expect(mockCreateObjectURL).toHaveBeenCalled();
|
||||
const blobArg = mockCreateObjectURL.mock.calls[0][0];
|
||||
expect(blobArg).toBeInstanceOf(Blob);
|
||||
expect(blobArg.type).toBe('text/calendar;charset=utf-8');
|
||||
});
|
||||
it("creates and downloads ICS file", () => {
|
||||
const med = createTestMed();
|
||||
|
||||
it('handles medication with multiple blisters', () => {
|
||||
const med = createTestMed({
|
||||
blisters: [
|
||||
{ usage: 1, every: 1, start: '2024-03-15T09:00:00' },
|
||||
{ usage: 2, every: 7, start: '2024-03-15T21:00:00' }
|
||||
]
|
||||
});
|
||||
|
||||
expect(() => generateICS(med)).not.toThrow();
|
||||
expect(mockCreateObjectURL).toHaveBeenCalled();
|
||||
});
|
||||
generateICS(med);
|
||||
|
||||
it('handles medication without optional fields', () => {
|
||||
const med = createTestMed({
|
||||
genericName: undefined,
|
||||
pillWeightMg: undefined,
|
||||
takenBy: [],
|
||||
notes: undefined
|
||||
});
|
||||
|
||||
expect(() => generateICS(med)).not.toThrow();
|
||||
});
|
||||
expect(mockCreateObjectURL).toHaveBeenCalledTimes(1);
|
||||
expect(mockAppendChild).toHaveBeenCalledTimes(1);
|
||||
expect(mockClick).toHaveBeenCalledTimes(1);
|
||||
expect(mockRemoveChild).toHaveBeenCalledTimes(1);
|
||||
expect(mockRevokeObjectURL).toHaveBeenCalledWith("blob:test-url");
|
||||
});
|
||||
|
||||
it('handles medication with empty blisters', () => {
|
||||
const med = createTestMed({ blisters: [] });
|
||||
|
||||
expect(() => generateICS(med)).not.toThrow();
|
||||
});
|
||||
it("generates correct filename", () => {
|
||||
const med = createTestMed({ name: "Test Med/Special" });
|
||||
|
||||
it('handles plural pills correctly', () => {
|
||||
const singlePillMed = createTestMed({
|
||||
blisters: [{ usage: 1, every: 1, start: '2024-03-15T09:00:00' }]
|
||||
});
|
||||
|
||||
const multiPillMed = createTestMed({
|
||||
blisters: [{ usage: 2, every: 1, start: '2024-03-15T09:00:00' }]
|
||||
});
|
||||
|
||||
expect(() => generateICS(singlePillMed)).not.toThrow();
|
||||
expect(() => generateICS(multiPillMed)).not.toThrow();
|
||||
});
|
||||
generateICS(med);
|
||||
|
||||
it('handles different interval values', () => {
|
||||
const dailyMed = createTestMed({
|
||||
blisters: [{ usage: 1, every: 1, start: '2024-03-15T09:00:00' }]
|
||||
});
|
||||
|
||||
const weeklyMed = createTestMed({
|
||||
blisters: [{ usage: 1, every: 7, start: '2024-03-15T09:00:00' }]
|
||||
});
|
||||
|
||||
expect(() => generateICS(dailyMed)).not.toThrow();
|
||||
expect(() => generateICS(weeklyMed)).not.toThrow();
|
||||
});
|
||||
expect(createdLink?.download).toBe("Test_Med_Special_schedule.ics");
|
||||
});
|
||||
|
||||
it("creates blob with text/calendar content type", () => {
|
||||
const med = createTestMed();
|
||||
|
||||
generateICS(med);
|
||||
|
||||
expect(mockCreateObjectURL).toHaveBeenCalled();
|
||||
const blobArg = mockCreateObjectURL.mock.calls[0][0];
|
||||
expect(blobArg).toBeInstanceOf(Blob);
|
||||
expect(blobArg.type).toBe("text/calendar;charset=utf-8");
|
||||
});
|
||||
|
||||
it("handles medication with multiple blisters", () => {
|
||||
const med = createTestMed({
|
||||
blisters: [
|
||||
{ usage: 1, every: 1, start: "2024-03-15T09:00:00" },
|
||||
{ usage: 2, every: 7, start: "2024-03-15T21:00:00" },
|
||||
],
|
||||
});
|
||||
|
||||
expect(() => generateICS(med)).not.toThrow();
|
||||
expect(mockCreateObjectURL).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("handles medication without optional fields", () => {
|
||||
const med = createTestMed({
|
||||
genericName: undefined,
|
||||
pillWeightMg: undefined,
|
||||
takenBy: [],
|
||||
notes: undefined,
|
||||
});
|
||||
|
||||
expect(() => generateICS(med)).not.toThrow();
|
||||
});
|
||||
|
||||
it("handles medication with empty blisters", () => {
|
||||
const med = createTestMed({ blisters: [] });
|
||||
|
||||
expect(() => generateICS(med)).not.toThrow();
|
||||
});
|
||||
|
||||
it("handles plural pills correctly", () => {
|
||||
const singlePillMed = createTestMed({
|
||||
blisters: [{ usage: 1, every: 1, start: "2024-03-15T09:00:00" }],
|
||||
});
|
||||
|
||||
const multiPillMed = createTestMed({
|
||||
blisters: [{ usage: 2, every: 1, start: "2024-03-15T09:00:00" }],
|
||||
});
|
||||
|
||||
expect(() => generateICS(singlePillMed)).not.toThrow();
|
||||
expect(() => generateICS(multiPillMed)).not.toThrow();
|
||||
});
|
||||
|
||||
it("handles different interval values", () => {
|
||||
const dailyMed = createTestMed({
|
||||
blisters: [{ usage: 1, every: 1, start: "2024-03-15T09:00:00" }],
|
||||
});
|
||||
|
||||
const weeklyMed = createTestMed({
|
||||
blisters: [{ usage: 1, every: 7, start: "2024-03-15T09:00:00" }],
|
||||
});
|
||||
|
||||
expect(() => generateICS(dailyMed)).not.toThrow();
|
||||
expect(() => generateICS(weeklyMed)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,183 +1,174 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
userStorageKey,
|
||||
todayIso,
|
||||
plusDaysIso,
|
||||
loadCollapsedDaysFromStorage,
|
||||
saveCollapsedDaysToStorage,
|
||||
getStoredTheme,
|
||||
saveTheme
|
||||
} from '../../utils/storage';
|
||||
getStoredTheme,
|
||||
loadCollapsedDaysFromStorage,
|
||||
plusDaysIso,
|
||||
saveCollapsedDaysToStorage,
|
||||
saveTheme,
|
||||
todayIso,
|
||||
userStorageKey,
|
||||
} from "../../utils/storage";
|
||||
|
||||
describe('userStorageKey', () => {
|
||||
it('generates user-specific storage key', () => {
|
||||
expect(userStorageKey(123, 'testKey')).toBe('testKey_user_123');
|
||||
});
|
||||
describe("userStorageKey", () => {
|
||||
it("generates user-specific storage key", () => {
|
||||
expect(userStorageKey(123, "testKey")).toBe("testKey_user_123");
|
||||
});
|
||||
|
||||
it('works with string userId', () => {
|
||||
expect(userStorageKey('456', 'myKey')).toBe('myKey_user_456');
|
||||
});
|
||||
it("works with string userId", () => {
|
||||
expect(userStorageKey("456", "myKey")).toBe("myKey_user_456");
|
||||
});
|
||||
});
|
||||
|
||||
describe('todayIso', () => {
|
||||
it('returns today date in ISO format', () => {
|
||||
const result = todayIso();
|
||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||||
|
||||
const today = new Date();
|
||||
const year = today.getFullYear();
|
||||
const month = String(today.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(today.getDate()).padStart(2, '0');
|
||||
expect(result).toBe(`${year}-${month}-${day}`);
|
||||
});
|
||||
describe("todayIso", () => {
|
||||
it("returns today date in ISO format", () => {
|
||||
const result = todayIso();
|
||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||||
|
||||
const today = new Date();
|
||||
const year = today.getFullYear();
|
||||
const month = String(today.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(today.getDate()).padStart(2, "0");
|
||||
expect(result).toBe(`${year}-${month}-${day}`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('plusDaysIso', () => {
|
||||
it('returns date N days from today', () => {
|
||||
const today = new Date();
|
||||
const expectedDate = new Date(today);
|
||||
expectedDate.setDate(expectedDate.getDate() + 7);
|
||||
|
||||
const result = plusDaysIso(7);
|
||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||||
|
||||
const year = expectedDate.getFullYear();
|
||||
const month = String(expectedDate.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(expectedDate.getDate()).padStart(2, '0');
|
||||
expect(result).toBe(`${year}-${month}-${day}`);
|
||||
});
|
||||
describe("plusDaysIso", () => {
|
||||
it("returns date N days from today", () => {
|
||||
const today = new Date();
|
||||
const expectedDate = new Date(today);
|
||||
expectedDate.setDate(expectedDate.getDate() + 7);
|
||||
|
||||
it('handles zero days', () => {
|
||||
expect(plusDaysIso(0)).toBe(todayIso());
|
||||
});
|
||||
const result = plusDaysIso(7);
|
||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||||
|
||||
it('handles negative days', () => {
|
||||
const today = new Date();
|
||||
const expectedDate = new Date(today);
|
||||
expectedDate.setDate(expectedDate.getDate() - 3);
|
||||
|
||||
const result = plusDaysIso(-3);
|
||||
const year = expectedDate.getFullYear();
|
||||
const month = String(expectedDate.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(expectedDate.getDate()).padStart(2, '0');
|
||||
expect(result).toBe(`${year}-${month}-${day}`);
|
||||
});
|
||||
const year = expectedDate.getFullYear();
|
||||
const month = String(expectedDate.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(expectedDate.getDate()).padStart(2, "0");
|
||||
expect(result).toBe(`${year}-${month}-${day}`);
|
||||
});
|
||||
|
||||
it("handles zero days", () => {
|
||||
expect(plusDaysIso(0)).toBe(todayIso());
|
||||
});
|
||||
|
||||
it("handles negative days", () => {
|
||||
const today = new Date();
|
||||
const expectedDate = new Date(today);
|
||||
expectedDate.setDate(expectedDate.getDate() - 3);
|
||||
|
||||
const result = plusDaysIso(-3);
|
||||
const year = expectedDate.getFullYear();
|
||||
const month = String(expectedDate.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(expectedDate.getDate()).padStart(2, "0");
|
||||
expect(result).toBe(`${year}-${month}-${day}`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadCollapsedDaysFromStorage', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue(null);
|
||||
});
|
||||
describe("loadCollapsedDaysFromStorage", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue(null);
|
||||
});
|
||||
|
||||
it('returns empty sets when no data in storage', () => {
|
||||
const result = loadCollapsedDaysFromStorage('collapsed', 'expanded');
|
||||
expect(result.collapsed.size).toBe(0);
|
||||
expect(result.expanded.size).toBe(0);
|
||||
});
|
||||
it("returns empty sets when no data in storage", () => {
|
||||
const result = loadCollapsedDaysFromStorage("collapsed", "expanded");
|
||||
expect(result.collapsed.size).toBe(0);
|
||||
expect(result.expanded.size).toBe(0);
|
||||
});
|
||||
|
||||
it('loads collapsed days from localStorage', () => {
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>)
|
||||
.mockImplementation((key: string) => {
|
||||
if (key === 'collapsed') return JSON.stringify(['2024-01-01', '2024-01-02']);
|
||||
return null;
|
||||
});
|
||||
it("loads collapsed days from localStorage", () => {
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockImplementation((key: string) => {
|
||||
if (key === "collapsed") return JSON.stringify(["2024-01-01", "2024-01-02"]);
|
||||
return null;
|
||||
});
|
||||
|
||||
const result = loadCollapsedDaysFromStorage('collapsed', 'expanded');
|
||||
expect(result.collapsed.has('2024-01-01')).toBe(true);
|
||||
expect(result.collapsed.has('2024-01-02')).toBe(true);
|
||||
expect(result.collapsed.size).toBe(2);
|
||||
});
|
||||
const result = loadCollapsedDaysFromStorage("collapsed", "expanded");
|
||||
expect(result.collapsed.has("2024-01-01")).toBe(true);
|
||||
expect(result.collapsed.has("2024-01-02")).toBe(true);
|
||||
expect(result.collapsed.size).toBe(2);
|
||||
});
|
||||
|
||||
it('loads expanded days from localStorage', () => {
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>)
|
||||
.mockImplementation((key: string) => {
|
||||
if (key === 'expanded') return JSON.stringify(['2024-01-03']);
|
||||
return null;
|
||||
});
|
||||
it("loads expanded days from localStorage", () => {
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockImplementation((key: string) => {
|
||||
if (key === "expanded") return JSON.stringify(["2024-01-03"]);
|
||||
return null;
|
||||
});
|
||||
|
||||
const result = loadCollapsedDaysFromStorage('collapsed', 'expanded');
|
||||
expect(result.expanded.has('2024-01-03')).toBe(true);
|
||||
expect(result.expanded.size).toBe(1);
|
||||
});
|
||||
const result = loadCollapsedDaysFromStorage("collapsed", "expanded");
|
||||
expect(result.expanded.has("2024-01-03")).toBe(true);
|
||||
expect(result.expanded.size).toBe(1);
|
||||
});
|
||||
|
||||
it('handles invalid JSON gracefully', () => {
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>)
|
||||
.mockReturnValue('invalid-json');
|
||||
it("handles invalid JSON gracefully", () => {
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue("invalid-json");
|
||||
|
||||
const result = loadCollapsedDaysFromStorage('collapsed', 'expanded');
|
||||
expect(result.collapsed.size).toBe(0);
|
||||
expect(result.expanded.size).toBe(0);
|
||||
});
|
||||
const result = loadCollapsedDaysFromStorage("collapsed", "expanded");
|
||||
expect(result.collapsed.size).toBe(0);
|
||||
expect(result.expanded.size).toBe(0);
|
||||
});
|
||||
|
||||
it('handles non-array JSON gracefully', () => {
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>)
|
||||
.mockReturnValue('{"not": "array"}');
|
||||
it("handles non-array JSON gracefully", () => {
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue('{"not": "array"}');
|
||||
|
||||
const result = loadCollapsedDaysFromStorage('collapsed', 'expanded');
|
||||
expect(result.collapsed.size).toBe(0);
|
||||
});
|
||||
const result = loadCollapsedDaysFromStorage("collapsed", "expanded");
|
||||
expect(result.collapsed.size).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveCollapsedDaysToStorage', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
describe("saveCollapsedDaysToStorage", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('saves state to localStorage', () => {
|
||||
const state = { '2024-01-01': true, '2024-01-02': false };
|
||||
saveCollapsedDaysToStorage('testKey', state);
|
||||
|
||||
expect(window.localStorage.setItem).toHaveBeenCalledWith(
|
||||
'testKey',
|
||||
JSON.stringify(state)
|
||||
);
|
||||
});
|
||||
it("saves state to localStorage", () => {
|
||||
const state = { "2024-01-01": true, "2024-01-02": false };
|
||||
saveCollapsedDaysToStorage("testKey", state);
|
||||
|
||||
it('handles storage errors gracefully', () => {
|
||||
(window.localStorage.setItem as ReturnType<typeof vi.fn>)
|
||||
.mockImplementation(() => {
|
||||
throw new Error('Storage full');
|
||||
});
|
||||
expect(window.localStorage.setItem).toHaveBeenCalledWith("testKey", JSON.stringify(state));
|
||||
});
|
||||
|
||||
// Should not throw
|
||||
expect(() => {
|
||||
saveCollapsedDaysToStorage('testKey', { key: true });
|
||||
}).not.toThrow();
|
||||
});
|
||||
it("handles storage errors gracefully", () => {
|
||||
(window.localStorage.setItem as ReturnType<typeof vi.fn>).mockImplementation(() => {
|
||||
throw new Error("Storage full");
|
||||
});
|
||||
|
||||
// Should not throw
|
||||
expect(() => {
|
||||
saveCollapsedDaysToStorage("testKey", { key: true });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getStoredTheme', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue(null);
|
||||
});
|
||||
describe("getStoredTheme", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue(null);
|
||||
});
|
||||
|
||||
it('returns "dark" as default', () => {
|
||||
expect(getStoredTheme()).toBe('dark');
|
||||
});
|
||||
it('returns "dark" as default', () => {
|
||||
expect(getStoredTheme()).toBe("dark");
|
||||
});
|
||||
|
||||
it('returns stored theme', () => {
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>)
|
||||
.mockReturnValue('light');
|
||||
expect(getStoredTheme()).toBe('light');
|
||||
});
|
||||
it("returns stored theme", () => {
|
||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue("light");
|
||||
expect(getStoredTheme()).toBe("light");
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveTheme', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// Reset mock to default behavior
|
||||
(window.localStorage.setItem as ReturnType<typeof vi.fn>).mockImplementation(() => {});
|
||||
});
|
||||
describe("saveTheme", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// Reset mock to default behavior
|
||||
(window.localStorage.setItem as ReturnType<typeof vi.fn>).mockImplementation(() => {});
|
||||
});
|
||||
|
||||
it('saves theme to localStorage', () => {
|
||||
saveTheme('light');
|
||||
expect(window.localStorage.setItem).toHaveBeenCalledWith('theme', 'light');
|
||||
});
|
||||
it("saves theme to localStorage", () => {
|
||||
saveTheme("light");
|
||||
expect(window.localStorage.setItem).toHaveBeenCalledWith("theme", "light");
|
||||
});
|
||||
|
||||
it('saves dark theme', () => {
|
||||
saveTheme('dark');
|
||||
expect(window.localStorage.setItem).toHaveBeenCalledWith('theme', 'dark');
|
||||
});
|
||||
it("saves dark theme", () => {
|
||||
saveTheme("dark");
|
||||
expect(window.localStorage.setItem).toHaveBeenCalledWith("theme", "dark");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user