Files

120 lines
2.8 KiB
JavaScript

jest.mock('../src/services/graphqlClient', () => ({
request: jest.fn()
}));
const express = require('express');
const request = require('supertest');
const graphqlClient = require('../src/services/graphqlClient');
const offersRouter = require('../routes/offers');
describe('Offers route', () => {
let app;
beforeEach(() => {
app = express();
app.use(express.json());
app.use('/api/ordrestyring/offers', offersRouter);
jest.clearAllMocks();
});
test('maps FinalReview payload into customer, offer, and line mutations', async () => {
graphqlClient.request
.mockResolvedValueOnce({
createCustomer: {
id: 42,
name: 'Mikael Holck',
email: '[email protected]'
}
})
.mockResolvedValueOnce({
createOffer: {
id: 88,
number: 'T-1001'
}
})
.mockResolvedValueOnce({
createOfferLines: [
{ id: 1 },
{ id: 2 }
]
});
const payload = {
project: {
id: 7,
name: 'Tagrenovering',
customer: 'Mikael Holck',
customerNumber: 'C100',
customerEmail: '[email protected]',
customerPhone: '42468110',
customerAddress: 'Hornumvej 7, 4600 Køge',
description: 'Renovering af tag'
},
geometry: {
roofArea: 132
},
package: {
materials: [
{
name: 'B7 plader',
quantity: 10,
unitPrice: 100,
varenr: 'B7-1'
}
],
laborTasks: [
{
name: 'Montage',
description: 'Montering af plader',
totalHours: 4,
rate: 580
}
],
totals: {
total: 3320
}
},
quote: {
validUntil: '2026-04-30T00:00:00.000Z'
}
};
const response = await request(app)
.post('/api/ordrestyring/offers/create')
.send(payload);
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
success: true,
offerNumber: 'T-1001',
offerId: 88,
customerId: 42,
lineCount: 2
});
expect(graphqlClient.request).toHaveBeenCalledTimes(3);
const createOfferCall = graphqlClient.request.mock.calls[1];
expect(createOfferCall[1].input).toMatchObject({
customerId: 42,
description: 'Tagrenovering',
reference: 'TG-C100-7'
});
const createLinesCall = graphqlClient.request.mock.calls[2];
expect(createLinesCall[1].inputs).toHaveLength(2);
expect(createLinesCall[1].inputs[0]).toMatchObject({
offerId: 88,
description: 'B7 plader',
quantity: 10,
salesPrice: 100
});
expect(createLinesCall[1].inputs[1]).toMatchObject({
offerId: 88,
description: 'Montering af plader',
quantity: 4,
salesPrice: 580
});
});
});