Files
FarmaFinder/apps/backend/__tests__/pharmacy-hours-endpoint.test.js
T
Antoni Nuñez Romeu e3e7d2f60b
Run Tests on Branches / Detect Changes (push) Successful in 17s
Run Tests on Branches / Backend Tests (push) Successful in 2m44s
Run Tests on Branches / Frontend Tests (push) Successful in 1m59s
Run Tests on Branches / Frontend Mobile Tests (push) Has been skipped
Run Tests on Branches / Parapharmacy API Tests (push) Has been skipped
Run Tests on Branches / PIP Platform Tests (push) Has been skipped
feat: pharmacy hours status and open now filter
- Add is_open_now and is_always_open helpers in backend
- Add backend endpoint enrichment with is_open and is_24h fields
- Add client-side getOpenStatus with 24h, opens-at, opens-tomorrow support
- Add open-now filter toggle in PublicView and SearchView
- Add pharmacy no-hours fallback display
- Add sticky pharmacy controls on scroll
- Add admin hours editor with 24h toggle
- Add translations (es/ca) for all hour-related strings
- Add backend tests for hours logic and pharmacy endpoint
- Remove unused backup test files
2026-07-27 14:32:05 +02:00

106 lines
3.4 KiB
JavaScript

import { jest } from '@jest/globals'
jest.unstable_mockModule('../cima-service.js', () => ({
searchMedicines: jest.fn(async () => []),
getMedicineDetails: jest.fn(async () => null),
searchOTC: jest.fn(async () => []),
}))
jest.unstable_mockModule('../farmacias-webhook-import.js', () => ({
runFarmaciaWebhookImport: jest.fn(async () => ({})),
DEFAULT_FARMACIAS_WEBHOOK: '',
importPharmaciesFromRows: jest.fn(async () => ({})),
}))
jest.unstable_mockModule('../../API/index.js', () => ({
fetchPharmaciesExternal: jest.fn(async () => []),
}))
process.env.DATABASE_PATH = ':memory:'
process.env.NODE_ENV = 'test'
const { default: supertest } = await import('supertest')
const { app, initDatabase, db } = await import('../server.js')
const HOURS_24_7 = JSON.stringify({
mon: ['00:00', '24:00'],
tue: ['00:00', '24:00'],
wed: ['00:00', '24:00'],
thu: ['00:00', '24:00'],
fri: ['00:00', '24:00'],
sat: ['00:00', '24:00'],
sun: ['00:00', '24:00'],
})
const HOURS_NORMAL = JSON.stringify({
mon: ['09:00', '21:00'],
tue: ['09:00', '21:00'],
wed: ['09:00', '21:00'],
thu: ['09:00', '21:00'],
fri: ['09:00', '21:00'],
sat: ['09:00', '14:00'],
sun: null,
})
function insertPharmacy(name, openingHours) {
return new Promise((resolve, reject) => {
db.run(
'INSERT INTO pharmacies (name, address, latitude, longitude, opening_hours) VALUES (?, ?, ?, ?, ?)',
[name, 'Address of ' + name, 41.5, 2.0, openingHours],
function (err) { return err ? reject(err) : resolve(this.lastID) }
)
})
}
beforeAll(async () => {
await initDatabase()
})
afterEach(async () => {
await new Promise((resolve, reject) => {
db.run('DELETE FROM pharmacies', (err) => (err ? reject(err) : resolve()))
})
})
describe('GET /api/pharmacies — is_open / is_24h enrichment', () => {
test('24/7 pharmacy → is_open=true, is_24h=true', async () => {
await insertPharmacy('24h Pharmacy', HOURS_24_7)
const res = await supertest(app).get('/api/pharmacies')
expect(res.status).toBe(200)
expect(res.body.length).toBe(1)
expect(res.body[0].is_open).toBe(true)
expect(res.body[0].is_24h).toBe(true)
})
test('normal hours pharmacy at an open time → is_open=true, is_24h=false', async () => {
await insertPharmacy('Normal Pharmacy', HOURS_NORMAL)
const res = await supertest(app).get('/api/pharmacies')
expect(res.status).toBe(200)
expect(res.body[0].is_24h).toBe(false)
expect(typeof res.body[0].is_open).toBe('boolean')
})
test('null opening_hours → is_open=null, is_24h=false', async () => {
await insertPharmacy('No Hours', null)
const res = await supertest(app).get('/api/pharmacies')
expect(res.status).toBe(200)
expect(res.body[0].is_open).toBeNull()
expect(res.body[0].is_24h).toBe(false)
})
test('multiple pharmacies each get correct enrichment', async () => {
await insertPharmacy('24h Pharmacy', HOURS_24_7)
await insertPharmacy('Normal Pharmacy', HOURS_NORMAL)
await insertPharmacy('No Hours', null)
const res = await supertest(app).get('/api/pharmacies')
expect(res.status).toBe(200)
expect(res.body.length).toBe(3)
const byName = Object.fromEntries(res.body.map(p => [p.name, p]))
expect(byName['24h Pharmacy'].is_open).toBe(true)
expect(byName['24h Pharmacy'].is_24h).toBe(true)
expect(byName['Normal Pharmacy'].is_24h).toBe(false)
expect(byName['No Hours'].is_open).toBeNull()
expect(byName['No Hours'].is_24h).toBe(false)
})
})