fix karung view data and sync only sync with the current date

This commit is contained in:
Alberto-Audrix committed 2026-09-04 09:33:17 +07:00
1 parent b3556c6ef4
commit 5ae477a99d
8 files changed
+396 -32

No files matched your search

@@ -1,8 +1,10 @@
import { render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { api } from '../../services/apiClient.ts';
import FeedSackCountingPage from '../FeedSackCountingPage.tsx';
const { karungRows, manualRows, cycle } = vi.hoisted(() => {
const { karungRows, manualRows, cycle, mismatchedKarungRows, mismatchedManualRows } = vi.hoisted(
() => {
const cycle = {
id: 1,
kandang: 1,
@@ -14,6 +16,7 @@ const { karungRows, manualRows, cycle } = vi.hoisted(() => {
chick_in_weight: 45,
doc_in_count: 20000,
status: 'active',
feed_initial_balance: 0,
created_at: '',
updated_at: '',
};
@@ -105,7 +108,69 @@ const { karungRows, manualRows, cycle } = vi.hoisted(() => {
updated_at: '',
},
];
return { karungRows, manualRows, cycle };
// IoT already on the newer day; manual only exists for the previous day.
const mismatchedKarungRows = [
{
id: 10,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-09-02',
in_today: 192,
out_today: 1,
in_total: 192,
out_total: 1,
feed_use_today: 7,
feed_use_total: 7,
created_at: '',
updated_at: '',
},
{
id: 11,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-09-03',
in_today: 50,
out_today: 0,
in_total: 242,
out_total: 1,
feed_use_today: 4,
feed_use_total: 11,
created_at: '',
updated_at: '',
},
];
const mismatchedManualRows = [
{
id: 10,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-09-02',
age_manual: 0,
mortality_manual: 0,
mortality_manual_total: 0,
feed_in_manual: 192,
feed_use_manual: 7,
feed_in_manual_total: 192,
feed_use_manual_total: 7,
feed_out_manual: 1,
feed_out_manual_total: 1,
harvest_manual: 0,
harvest_manual_total: 0,
harvest_weight_manual: 0,
harvest_weight_manual_total: 0,
manual_weight: 0,
average_harvest_day_manual: 0,
average_harvest_weight_manual: 0,
fcr_manual: 0,
eef_manual: 0,
created_at: '',
updated_at: '',
},
];
return { karungRows, manualRows, cycle, mismatchedKarungRows, mismatchedManualRows };
});
vi.mock('../../services/apiClient.ts', () => ({
@@ -137,6 +202,7 @@ vi.mock('../../context/FarmContext.tsx', () => ({
selectedCycle: cycle,
cycles: [cycle],
setSelectedCycleId: vi.fn(),
updateCycle: vi.fn(),
}),
}));
@@ -147,6 +213,12 @@ class ResizeObserverStub {
}
describe('FeedSackCountingPage', () => {
beforeEach(() => {
vi.mocked(api.karungs.list).mockResolvedValue(karungRows);
vi.mocked(api.manualInputs.list).mockResolvedValue(manualRows);
vi.mocked(api.insights.list).mockResolvedValue([]);
});
it('renders the recreated hitung karung view with stats, saldo, and daily table', async () => {
vi.stubGlobal('ResizeObserver', ResizeObserverStub);
try {
@@ -192,4 +264,25 @@ describe('FeedSackCountingPage', () => {
vi.unstubAllGlobals();
}
});
it('does not reuse yesterday manual values on the latest IoT day stats cards', async () => {
vi.stubGlobal('ResizeObserver', ResizeObserverStub);
vi.mocked(api.karungs.list).mockResolvedValue(mismatchedKarungRows);
vi.mocked(api.manualInputs.list).mockResolvedValue(mismatchedManualRows);
try {
render(<FeedSackCountingPage onSettings={() => undefined} />);
expect(await screen.findByRole('heading', { name: 'Hitung Karung' })).toBeInTheDocument();
// Latest IoT day still shows.
expect(screen.getAllByText('50').length).toBeGreaterThan(0);
// Summary Manual cards show 0 when the latest IoT day has no matching manual row.
expect(screen.getAllByText('0').length).toBeGreaterThanOrEqual(6);
// Daily table still keeps the real 2 Sep manual row across masuk/tuang/keluar boxes.
expect(screen.getAllByText('192').length).toBeGreaterThan(0);
expect(screen.getAllByText('3 Sep 2026').length).toBeGreaterThan(0);
expect(screen.getAllByText('2 Sep 2026').length).toBeGreaterThan(0);
} finally {
vi.unstubAllGlobals();
}
});
});