Skip to content

Commit

Permalink
Merge pull request #57 from HubSpot/br-unit-tests-7
Browse files Browse the repository at this point in the history
Unit tests for custom object utils
  • Loading branch information
brandenrodgers authored Nov 3, 2023
2 parents 246d9c4 + 09b3f55 commit 078422e
Show file tree
Hide file tree
Showing 6 changed files with 1,570 additions and 44 deletions.
106 changes: 106 additions & 0 deletions lib/__tests__/customObjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import fs from 'fs-extra';
import {
downloadSchema,
downloadSchemas,
writeSchemaToDisk,
} from '../customObjects';
import {
fetchObjectSchema as __fetchObjectSchema,
fetchObjectSchemas as __fetchObjectSchemas,
} from '../../api/customObjects';
import basicSchema from './fixtures/customObjects/basicSchema.json';
import fullSchema from './fixtures/customObjects/fullSchema.json';
import multipleSchemas from './fixtures/customObjects/multipleSchemas.json';

jest.mock('fs-extra');
jest.mock('../../api/customObjects');

const outputFileSyncSpy = jest.spyOn(fs, 'outputFileSync');
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
const fetchObjectSchema = __fetchObjectSchema as jest.MockedFunction<
typeof __fetchObjectSchema
>;
const fetchObjectSchemas = __fetchObjectSchemas as jest.MockedFunction<
typeof __fetchObjectSchemas
>;

describe('lib/customObjects', () => {
beforeEach(() => {
outputFileSyncSpy.mockClear();
});

describe('writeSchemaToDisk()', () => {
it('writes schema to disk', async () => {
await writeSchemaToDisk(basicSchema);
const outputFileArgs = outputFileSyncSpy.mock.lastCall;

expect(
outputFileArgs ? JSON.parse(outputFileArgs[1] as string) : null
).toEqual(expect.objectContaining(basicSchema));
});
});

describe('downloadSchema()', () => {
it('downloads a schema and writes it to disk', async () => {
fetchObjectSchema.mockResolvedValue(fullSchema);

const result = await downloadSchema(123, '');
const outputFileArgs = outputFileSyncSpy.mock.lastCall;

expect(result).toEqual(expect.objectContaining(fullSchema));
expect(
outputFileArgs ? JSON.parse(outputFileArgs[1] as string) : null
).toEqual(expect.objectContaining(fullSchema));
});

it('handles schema fetch errors', async () => {
fetchObjectSchema.mockImplementation(() => {
return Promise.reject('Some error');
});

let errorNotThrown = true;
try {
await downloadSchema(123, '');
} catch (err) {
errorNotThrown = false;
expect(err).toBeDefined();
}

if (errorNotThrown) {
throw new Error('Expected downloadSchema to throw an error');
}
});
});

describe('downloadSchemas()', () => {
it('downloads schemas and writes them to disk', async () => {
fetchObjectSchemas.mockResolvedValue({ results: multipleSchemas });

const result = await downloadSchemas(123);
expect(result).toEqual(expect.objectContaining(multipleSchemas));

outputFileSyncSpy.mock.calls.forEach((call, i) => {
const schemaArg = JSON.parse(call[1] as string);
expect(schemaArg).toEqual(expect.objectContaining(multipleSchemas[i]));
});
});

it('handles schemas fetch errors', async () => {
fetchObjectSchemas.mockImplementation(() => {
return Promise.reject('Some error');
});

let errorNotThrown = true;
try {
await downloadSchemas(123, '');
} catch (err) {
errorNotThrown = false;
expect(err).toBeDefined();
}

if (errorNotThrown) {
throw new Error('Expected downloadSchemas to throw an error');
}
});
});
});
26 changes: 26 additions & 0 deletions lib/__tests__/fixtures/customObjects/basicSchema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id": "basic-schema-id",
"labels": { "singular": "Schema", "plural": "Schemas" },
"associatedObjects": null,
"requiredProperties": [],
"searchableProperties": [],
"primaryDisplayProperty": "name",
"objectTypeId": "2-123",
"properties": [
{
"name": "name",
"label": "Name",
"type": "string",
"fieldType": "text",
"description": "Name Field"
}
],
"associations": [
{
"id": "some-associated-object",
"fromObjectTypeId": "string",
"toObjectTypeId": "string"
}
],
"name": "schema"
}
Loading

0 comments on commit 078422e

Please sign in to comment.