1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/test/frontend/__setup__/server.js

127 lines
4.3 KiB
JavaScript
Raw Normal View History

import { setupServer } from 'msw/node';
import { rest } from 'msw';
import { buildHistoryItem } from '../__fixtures__/settings';
import FixturesLib from '../__lib__/fixtures';
let fixtures = null;
2022-11-28 17:01:17 +01:00
export const server = setupServer(
rest.get('/api/groups', (req, res, ctx) => {
return res(ctx.json(fixtures.groups));
}),
rest.get('/api/plan_categories', (req, res, ctx) => {
return res(ctx.json(fixtures.planCategories));
}),
rest.get('/api/users', (req, res, ctx) => {
2022-11-29 10:48:23 +01:00
switch (new URLSearchParams(req.url.search).get('role')) {
case 'partner':
return res(ctx.json(fixtures.partners));
2022-11-29 10:48:23 +01:00
case 'manager':
return res(ctx.json(fixtures.managers));
2022-11-29 10:48:23 +01:00
default:
return res(ctx.json(fixtures.users));
2022-11-29 10:48:23 +01:00
}
}),
rest.get('/api/plans', (req, res, ctx) => {
return res(ctx.json(fixtures.plans));
}),
2022-11-29 10:48:23 +01:00
rest.post('/api/plans', (req, res, ctx) => {
return res(ctx.json(req.body));
}),
2023-01-26 10:04:47 +01:00
rest.put('/api/plans/:id', (req, res, ctx) => {
return res(ctx.json(req.body));
}),
rest.post('/api/users', (req, res, ctx) => {
/* eslint-disable camelcase */
const { user: { first_name, last_name, email } } = req.body;
return res(ctx.status(201), ctx.json({
id: Math.ceil(Math.random() * 100),
email,
profile_attributes: { first_name, last_name }
}));
/* eslint-enable camelcase */
}),
rest.get('/api/settings/:name', (req, res, ctx) => {
const setting = fixtures.settings.find(s => s.name === req.params.name);
2022-12-23 15:52:10 +01:00
const history = new URLSearchParams(req.url.search).get('history');
const result = { setting };
if (history) {
result.setting.history = [buildHistoryItem(setting)];
}
return res(ctx.json(result));
}),
rest.get('/api/settings', (req, res, ctx) => {
2022-12-20 16:47:46 +01:00
const names = new URLSearchParams(req.url.search).get('names');
const foundSettings = fixtures.settings.filter(setting => names.replace(/[[\]']/g, '').split(',').includes(setting.name));
2022-12-20 16:47:46 +01:00
return res(ctx.json(Object.fromEntries(foundSettings.map(s => [s.name, s.value]))));
2022-11-28 17:39:37 +01:00
}),
rest.patch('/api/settings/bulk_update', (req, res, ctx) => {
return res(ctx.json(req.body));
2022-12-05 17:09:42 +01:00
}),
rest.get('/api/product_categories', (req, res, ctx) => {
return res(ctx.json(fixtures.productCategories));
2022-12-05 17:09:42 +01:00
}),
rest.get('/api/products', (req, res, ctx) => {
return res(ctx.json(fixtures.products));
2022-12-05 17:09:42 +01:00
}),
rest.get('/api/products/:id/stock_movements', (req, res, ctx) => {
const { id } = req.params;
return res(ctx.json({
page: 1,
total_pages: Math.ceil(fixtures.productStockMovements.length / 10),
2022-12-05 17:09:42 +01:00
page_size: 10,
total_count: fixtures.productStockMovements.length,
data: fixtures.productStockMovements.filter(m => String(m.product_id) === id)
2022-12-05 17:09:42 +01:00
}));
}),
rest.get('/api/machines', (req, res, ctx) => {
return res(ctx.json(fixtures.machines));
2022-12-20 16:47:46 +01:00
}),
rest.get('/api/auth_providers/active', (req, res, ctx) => {
return res(ctx.json(fixtures.providers[0]));
2022-12-20 16:47:46 +01:00
}),
rest.get('/api/profile_custom_fields', (req, res, ctx) => {
return res(ctx.json(fixtures.profileCustomFields));
2022-12-20 16:47:46 +01:00
}),
rest.get('/api/members/current', (req, res, ctx) => {
return res(ctx.json(global.loggedUser));
2022-12-22 12:11:14 +01:00
}),
rest.get('/api/spaces', (req, res, ctx) => {
return res(ctx.json(fixtures.spaces));
}),
rest.get('/api/statuses', (req, res, ctx) => {
return res(ctx.json(fixtures.statuses));
}),
rest.delete('api/statuses/:id', (req, res, ctx) => {
const id = parseInt(req.params.id);
const statusIndex = fixtures.statuses.findIndex((status) => status.id === id);
fixtures.statuses.splice(statusIndex, 1);
return res(ctx.json());
}),
rest.patch('api/statuses/:id', async (req, res, ctx) => {
const id = parseInt(req.params.id);
const reqBody = await req.json();
const status = fixtures.statuses.find((status) => status.id === id);
status.label = reqBody.status.label;
return res(ctx.json(status));
}),
rest.post('/api/statuses', async (req, res, ctx) => {
const reqBody = await req.json();
const status = reqBody.status;
status.id = fixtures.statuses.length + 1;
fixtures.statuses.push(status);
return res(ctx.json({ status }));
})
);
beforeAll(() => {
server.listen();
fixtures = FixturesLib.init();
}
);
afterEach(() => {
server.resetHandlers();
fixtures = FixturesLib.init();
});
afterAll(() => server.close());