Skip to content

Commit

Permalink
Update test data for form attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-white committed Sep 4, 2018
1 parent e26cd49 commit ee1156b
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions test/data/form-attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,46 @@ import faker from '../faker';
import { dataStore } from './data-store';
import { extendedForms } from './forms';

const FORM_ATTACHMENT_TYPES = [
{ type: 'image', fileExtension: 'jpg' },
{ type: 'audio', fileExtension: 'mp3' },
{ type: 'video', fileExtension: 'mp4' }
];
const TYPES = {
image: 'jpg',
audio: 'mp3',
video: 'mp4'
};

// eslint-disable-next-line import/prefer-default-export
export const extendedFormAttachments = dataStore({
factory: ({
inPast,
lastCreatedAt,
exists = faker.random.boolean()
type = faker.random.arrayElement(Object.keys(TYPES)),
// Adding a UUID to help ensure uniqueness.
name = `${faker.random.words()} ${faker.random.uuid()}${TYPES[type]}`,
...options
}) => {
if (!inPast && options.exists === true)
throw new Error('inPast and exists are inconsistent');
if (options.hasUpdatedAt != null) {
if (!inPast && options.hasUpdatedAt)
throw new Error('inPast and hasUpdatedAt are inconsistent');
if (options.exists === true && !options.hasUpdatedAt)
throw new Error('exists and hasUpdatedAt are inconsistent');
if (options.exists == null) {
// eslint-disable-next-line no-param-reassign
options.exists = options.hasUpdatedAt ? faker.random.boolean() : false;
}
} else {
if (options.exists == null) {
// eslint-disable-next-line no-param-reassign
options.exists = inPast && faker.random.boolean();
}
// eslint-disable-next-line no-param-reassign
options.hasUpdatedAt = inPast && (options.exists || faker.random.boolean());
}
const { exists, hasUpdatedAt } = options;
const form = extendedForms.randomOrCreatePast();
const type = faker.random.arrayElement(FORM_ATTACHMENT_TYPES);
const { updatedAt } = faker.date.timestamps(inPast, [
lastCreatedAt,
form.createdAt
]);
return {
type,
name: faker.system.commonFileName(type.fileExtension),
exists,
updatedAt
};
const updatedAt = hasUpdatedAt
? faker.date.pastSince(form.createdAt).toISOString()
: null;
return { type, name, exists, updatedAt };
},
sort: 'name'
});

0 comments on commit ee1156b

Please sign in to comment.