Skip to content

Commit

Permalink
update place-factory test
Browse files Browse the repository at this point in the history
  • Loading branch information
freddieptf committed Feb 19, 2025
1 parent 69b5081 commit 3246ef7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/routes/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import _ from 'lodash';
import PlaceFactory from '../services/place-factory';
import Validation from '../validation';
import { PlaceUploadState } from '../services/place';
import crypto from 'crypto';

export default async function newHandler(fastify: FastifyInstance) {

Expand Down Expand Up @@ -80,7 +81,7 @@ export default async function newHandler(fastify: FastifyInstance) {
return resp.view('src/liquid/new/place_list_fragment.liquid', {
contactType,
item: {
id: formData.place_name.toLowerCase().replaceAll(' ', '_'),
id: crypto.randomUUID(),
name: formData.place_name,
value: Buffer.from(JSON.stringify(req.body)).toString('base64'),
},
Expand Down
2 changes: 1 addition & 1 deletion src/services/place-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class PlaceFactory {
const places: Place[] = [];
list.forEach((data, idx) => {
const parsed = JSON.parse(Buffer.from(data, 'base64').toString('utf8'));
const placeData = { ...parsed, ...formData };
const placeData = { ...formData, ...parsed };
const place = new Place(contactType);
place.setPropertiesFromFormData(placeData, 'hierarchy_');
if (idx > 0) {
Expand Down
26 changes: 26 additions & 0 deletions test/services/place-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,32 @@ it('#165 - create a place when generated property is required', async () => {
expect(place.validationErrors).to.be.empty;
});

it('create multiple places with single contact', async () => {
const genPlaceFormData = (contactType, uniqfix) => {
const data = {};
contactType.place_properties.forEach(p => data[`place_${p.property_name}`] = p.property_name + uniqfix);
return data;
};

const { sessionCache, contactType, fakeFormData, chtApi } = mockScenario();
console.log(contactType.place_properties.map(p => p.property_name));

const placeCount = 3;
for (let i = 0; i < placeCount; i++) {
fakeFormData[`list_${i}`] = Buffer.from(JSON.stringify(genPlaceFormData(contactType, `${i}`))).toString('base64');
}
const places = await PlaceFactory.createManyWithSingleUser(fakeFormData, contactType, sessionCache, chtApi);
expect(places).lengthOf(placeCount);
const contact = places[0].contact.id;
for (let i = 0; i < placeCount; i++) {
const place = places[i];
expect(place.name).equals(`Name${i}`);
expect(place.validationErrors).to.be.empty;
expect(place.contact.id).equals(contact);
expect(place.resolvedHierarchy[1]?.id).to.eq('parent-id');
}
});

function mockScenario() {
const contactType = mockValidContactType('string', undefined);
const parentDoc: ChtDoc = {
Expand Down

0 comments on commit 3246ef7

Please sign in to comment.