Skip to content

Commit

Permalink
Created Document Type factory
Browse files Browse the repository at this point in the history
  • Loading branch information
targendaz2 committed Apr 23, 2024
1 parent 0972915 commit 6215a20
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 4 deletions.
7 changes: 6 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"version": "0.2",
"ignorePaths": [".prettierrc*", ".vscode/*", "*.lock", "node_modules/"],
"dictionaryDefinitions": [
{
"name": "extensions",
"description": "Common file extensions.",
"path": "./dicts/extensions.txt"
},
{
"name": "macOS",
"description": "macOS terms and concepts.",
Expand All @@ -20,7 +25,7 @@
],
"dictionaries": [
"bash",
"filetypes",
"extensions",
"macOS",
"MDMs",
"misc",
Expand Down
28 changes: 28 additions & 0 deletions dicts/extensions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
aac
css
csv
gif
gz
heic
heif
htm
html
jpeg
jpg
js
json
m4a
md
mp4
pdf
png
rtf
tgz
tif
tiff
txt
xhtml
xml
yaml
yml
zip
53 changes: 53 additions & 0 deletions tests/factories/documentType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { DocumentType } from '@/src/types';
import { faker } from '@faker-js/faker';
import { Factory } from 'fishery';

class DocumentTypeFactory extends Factory<DocumentType> {}

export const documentTypeFactory = DocumentTypeFactory.define(() => {
const [extension, mimeType] = faker.helpers.objectEntry({
aac: 'audio/aac',
css: 'text/css',
csv: 'text/csv',
gif: 'image/gif',
gz: 'application/gzip',
heic: 'image/heic',
heif: 'image/heic',
htm: 'text/html',
html: 'text/html',
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
js: 'text/javascript',
json: 'application/json',
m4a: 'audio/aac',
md: 'text/markdown',
mp4: 'video/mp4',
pdf: 'application/pdf',
png: 'image/png',
rtf: 'application/rtf',
tgz: 'application/gzip',
tif: 'image/tiff',
tiff: 'image/tiff',
txt: 'text/plain',
xhtml: 'application/xhtml+xml',
xml: 'application/xml',
yaml: 'application/yaml',
yml: 'application/yaml',
zip: 'application/zip',
});

const role = faker.helpers.arrayElement([
'Editor',
'Viewer',
'Shell',
'QLGenerator',
'None',
'All',
]);

return {
CFBundleTypeExtensions: [extension],
CFBundleTypeMIMETypes: [mimeType],
CFBundleTypeRole: role as DocumentType['CFBundleTypeRole'],
};
});
1 change: 1 addition & 0 deletions tests/factories/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { documentTypeFactory } from './documentType';
export { infoPlistFactory } from './infoPlist';
export { urlTypeFactory } from './urlType';
17 changes: 15 additions & 2 deletions tests/factories/infoPlist.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import type { InfoPlist } from '@/src/types';
import { urlTypeFactory } from '@/tests/factories/urlType';
import { documentTypeFactory, urlTypeFactory } from '@/tests/factories';
import { faker } from '@faker-js/faker';
import { Factory } from 'fishery';

class InfoPlistFactory extends Factory<InfoPlist, { urlTypes?: number }> {
class InfoPlistFactory extends Factory<
InfoPlist,
{ documentTypes?: number; urlTypes?: number }
> {
documentTypes(count: number = 1) {
return this.transient({ documentTypes: count });
}

urlTypes(count: number = 1) {
return this.transient({ urlTypes: count });
}
Expand All @@ -22,6 +29,12 @@ export const infoPlistFactory = InfoPlistFactory.define(
CFBundleShortVersionString: faker.system.semver(),
};

if (transientParams.documentTypes) {
infoPlist.CFBundleDocumentTypes = documentTypeFactory.buildList(
transientParams.documentTypes,
);
}

if (transientParams.urlTypes) {
infoPlist.CFBundleURLTypes = urlTypeFactory.buildList(
transientParams.urlTypes,
Expand Down
41 changes: 40 additions & 1 deletion tests/unit/factories.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
// import cp from 'node:child_process';
import { infoPlistFactory, urlTypeFactory } from '@/tests/factories';
import {
documentTypeFactory,
infoPlistFactory,
urlTypeFactory,
} from '@/tests/factories';
import { describe, expect, test } from '@jest/globals';

describe('Document Type factory tests', () => {
test('can generate an object', () => {
documentTypeFactory.build();
});

test('generated document type has a corresponding extension and MIME type', () => {
const documentType = documentTypeFactory.build();
const extension = documentType.CFBundleTypeExtensions![0];

expect(documentType.CFBundleTypeMIMETypes![0]).toContain(extension);
});

test('generated document type has a role', () => {
const documentType = documentTypeFactory.build();
expect(documentType.CFBundleTypeRole).toBeDefined;
});
});

describe('URL Type factory tests', () => {
test('can generate an object', () => {
urlTypeFactory.build();
Expand All @@ -26,6 +48,16 @@ describe('Info.plist factory tests', () => {
expect(infoPlist.CFBundleIdentifier).toContain(name);
});

test('can generate Info.plist with single Document Type', () => {
const infoPlist = infoPlistFactory.documentTypes().build();
expect(infoPlist.CFBundleDocumentTypes!.length).toBe(1);
});

test('can generate Info.plist with multiple Document Types', () => {
const infoPlist = infoPlistFactory.documentTypes(3).build();
expect(infoPlist.CFBundleDocumentTypes!.length).toBe(3);
});

test('can generate Info.plist with single URL Type', () => {
const infoPlist = infoPlistFactory.urlTypes().build();
expect(infoPlist.CFBundleURLTypes!.length).toBe(1);
Expand All @@ -35,4 +67,11 @@ describe('Info.plist factory tests', () => {
const infoPlist = infoPlistFactory.urlTypes(3).build();
expect(infoPlist.CFBundleURLTypes!.length).toBe(3);
});

test('can generate Info.plist with Document Types and URL Types', () => {
const infoPlist = infoPlistFactory.documentTypes(3).urlTypes(2).build();

expect(infoPlist.CFBundleDocumentTypes!.length).toBe(3);
expect(infoPlist.CFBundleURLTypes!.length).toBe(2);
});
});

0 comments on commit 6215a20

Please sign in to comment.