Skip to content

Commit

Permalink
fix(sites-28856): update shared-data schema to include type in options
Browse files Browse the repository at this point in the history
* include a data attribute on options
  • Loading branch information
Ben Helleman committed Feb 6, 2025
1 parent eabc054 commit 1c5c511
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ImportJob extends BaseModel {
ENABLE_JAVASCRIPT: 'enableJavascript',
PAGE_LOAD_TIMEOUT: 'pageLoadTimeout',
TYPE: 'type',
DATA: 'data',
};

static ImportOptionTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const ImportOptionTypeValidator = {
throw new Error(`Invalid value for ${ImportJob.ImportOptions.TYPE}: ${value}`);
}
},
[ImportJob.ImportOptions.DATA]: (value) => {
if (!isObject(value)) {
throw new Error(`Invalid value for ${ImportJob.ImportOptions.DATA}: ${value}`);
}
},
};

const validateOptions = (options) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,33 @@ describe('ImportJob IT', async () => {
});

it('adds a new import job with valid options', async () => {
let data = { ...newJobData, options: { type: 'xwalk' } };
const options = {
type: 'xwalk',
data: {
siteName: 'xwalk',
assetFolder: 'xwalk',
},
};

let data = { ...newJobData, options };
let importJob = await ImportJob.create(data);

checkImportJob(importJob);
expect(importJob.getOptions()).to.eql({ type: 'xwalk' });
expect(importJob.getOptions()).to.equal(data.options);

data = { ...newJobData, options: { type: 'doc' } };
importJob = await ImportJob.create(data);

checkImportJob(importJob);
expect(importJob.getOptions()).to.eql({ type: 'doc' });

// test to make sure data error is thrown if data is not an object
data = { ...newJobData, options: { data: 'not-an-object' } };
await ImportJob.create(data).catch((err) => {
expect(err).to.be.instanceOf(DataAccessError);
expect(err.cause).to.be.instanceOf(ElectroValidationError);
expect(err.cause.message).to.contain('Invalid value for data: not-an-object');
});
});

it('throws an error when adding a new import job with invalid options', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ describe('ImportJobModel', () => {
userAgent: 'someUserAgent',
},
options: {
someOption: 'someValue',
type: 'xwalk',
},
redirectCount: 0,
status: 'RUNNING',
startedAt: '2022-01-01T00:00:00.000Z',
successCount: 0,
urlCount: 0,
data: {
siteName: 'xwalk',
assetFolder: 'xwalk',
},
};

({
Expand Down Expand Up @@ -189,15 +193,26 @@ describe('ImportJobModel', () => {
});

describe('options', () => {
it('no options', () => {
instance.setOptions(undefined);
expect(instance.getOptions()).to.be.undefined;
});

it('gets options', () => {
expect(instance.getOptions()).to.deep.equal({ someOption: 'someValue' });
expect(instance.getOptions()).to.deep.equal({ type: 'xwalk' });
});

it('sets options', () => {
const newOptions = { newOption: 'newValue' };
instance.setOptions(newOptions);
expect(instance.getOptions()).to.deep.equal(newOptions);
});

it('sets options with data attribute', () => {
const newOptions = { data: { siteFolder: 'xwalk', assetFolder: 'xwalk' } };
instance.setOptions(newOptions);
expect(instance.getOptions()).to.deep.equal(newOptions);
});
});

describe('redirectCount', () => {
Expand Down

0 comments on commit 1c5c511

Please sign in to comment.