removed mocks

This commit is contained in:
2025-11-10 12:19:37 +01:00
parent eee254ff9f
commit 2e27395ed0
6 changed files with 28 additions and 290 deletions

View File

@@ -5,23 +5,6 @@ import { formatDateValue } from '../utils/dateUtils';
const API_URL = '/api/iobroker/pickup-config';
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const STATIC_CONFIG = [
{ id: '63448', active: false, checkProfileId: true, onlyNotify: true, label: 'Penny Baden-Oos' },
{ id: '44975', active: false, checkProfileId: true, onlyNotify: false, label: 'Aldi Kuppenheim', desiredWeekday: 'Samstag' },
{ id: '44972', active: false, checkProfileId: true, onlyNotify: false, label: 'Aldi Biblisweg', desiredWeekday: 'Dienstag' },
{
id: '44975',
active: false,
checkProfileId: true,
onlyNotify: false,
label: 'Aldi Kuppenheim',
desiredDateRange: { start: '2025-05-18', end: '2025-05-18' }
},
{ id: '33875', active: false, checkProfileId: true, onlyNotify: false, label: 'Cap Markt', desiredWeekday: 'Donnerstag' },
{ id: '42322', active: false, checkProfileId: false, onlyNotify: false, label: 'Edeka Haueneberstein' },
{ id: '51450', active: false, checkProfileId: true, onlyNotify: false, label: 'Hornbach Grünwinkel' }
];
const usePickupConfig = () => {
const [config, setConfig] = useState([]);
const [loading, setLoading] = useState(true);
@@ -33,11 +16,12 @@ const usePickupConfig = () => {
setError('');
try {
// In einer echten Implementierung würde hier die API aufgerufen:
// const response = await fetch(API_URL);
// const data = await response.json();
await delay(500);
setConfig(sortEntriesByLabel(STATIC_CONFIG));
const response = await fetch(API_URL);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
setConfig(sortEntriesByLabel(Array.isArray(data) ? data : []));
} catch (err) {
setError('Fehler beim Laden der Konfiguration: ' + err.message);
} finally {
@@ -50,9 +34,14 @@ const usePickupConfig = () => {
setError('');
try {
// API-Aufruf zum Speichern der Konfiguration:
// await fetch(API_URL, { method: 'POST', body: JSON.stringify(config) });
await delay(1000);
const response = await fetch(API_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(config)
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
setStatus('Konfiguration erfolgreich gespeichert!');
setTimeout(() => setStatus(''), 3000);
} catch (err) {

View File

@@ -3,6 +3,11 @@ import { act, render } from '@testing-library/react';
import usePickupConfig from './usePickupConfig';
describe('usePickupConfig', () => {
const mockConfig = [
{ id: '2', label: 'Beta Store', active: false, checkProfileId: true, onlyNotify: false },
{ id: '1', label: 'alpha Shop', active: false, checkProfileId: true, onlyNotify: false }
];
const advanceTimers = async (ms = 0) => {
await act(async () => {
jest.advanceTimersByTime(ms);
@@ -22,9 +27,16 @@ describe('usePickupConfig', () => {
beforeEach(() => {
jest.useFakeTimers();
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockConfig)
})
);
});
afterEach(() => {
global.fetch.mockRestore?.();
jest.useRealTimers();
});
@@ -33,8 +45,8 @@ describe('usePickupConfig', () => {
await advanceTimers(600);
const hook = getHook();
expect(hook.loading).toBe(false);
expect(hook.config[0].label).toBe('Aldi Biblisweg');
expect(hook.config[hook.config.length - 1].label).toBe('Penny Baden-Oos');
expect(hook.config[0].label).toBe('alpha Shop');
expect(hook.config[hook.config.length - 1].label).toBe('Beta Store');
});
test('mutators update entries as expected', async () => {