Skip to content

Commit

Permalink
Refactor: NewDatasetValidator unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Jan 12, 2024
1 parent 1b4b29a commit 32ce475
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions test/unit/datasets/NewDatasetValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ describe('execute', () => {
sandbox.restore();
});

test('should not raise validation error when new dataset is valid', async () => {
const testNewDataset = createNewDatasetModel();
function setupMetadataBlocksRepositoryStub(): IMetadataBlocksRepository {
const testMetadataBlock = createNewDatasetMetadataBlockModel();
const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{};
const getMetadataBlockByNameStub = sandbox.stub().resolves(testMetadataBlock);
metadataBlocksRepositoryStub.getMetadataBlockByName = getMetadataBlockByNameStub;
const sut = new NewDatasetValidator(metadataBlocksRepositoryStub);
return metadataBlocksRepositoryStub;
}

test('should not raise validation error when new dataset is valid', async () => {
const testNewDataset = createNewDatasetModel();
const sut = new NewDatasetValidator(setupMetadataBlocksRepositoryStub());

await sut.validate(testNewDataset).catch((e) => fail(e));
});

test('should raise an empty field error when a first level field is missing', async () => {
const testNewDataset = createNewDatasetModelWithoutFirstLevelRequiredField();
const testMetadataBlock = createNewDatasetMetadataBlockModel();
const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{};
const getMetadataBlockByNameStub = sandbox.stub().resolves(testMetadataBlock);
metadataBlocksRepositoryStub.getMetadataBlockByName = getMetadataBlockByNameStub;
const sut = new NewDatasetValidator(metadataBlocksRepositoryStub);
const sut = new NewDatasetValidator(setupMetadataBlocksRepositoryStub());

await sut
.validate(testNewDataset)
Expand All @@ -56,24 +56,20 @@ describe('execute', () => {
test('should raise an error when the provided field value for a multiple field is a string', async () => {
const invalidAuthorFieldValue = 'invalidValue';
const testNewDataset = createNewDatasetModel(invalidAuthorFieldValue);
const testMetadataBlock = createNewDatasetMetadataBlockModel();
const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{};
const getMetadataBlockByNameStub = sandbox.stub().resolves(testMetadataBlock);
metadataBlocksRepositoryStub.getMetadataBlockByName = getMetadataBlockByNameStub;
const sut = new NewDatasetValidator(metadataBlocksRepositoryStub);
const sut = new NewDatasetValidator(setupMetadataBlocksRepositoryStub());

await sut
.validate(testNewDataset)
.then(() => {
fail('Validation should fail');
})
.catch((error) => {
const emptyFieldError = error as FieldValidationError;
assert.match(emptyFieldError.citationBlockName, 'citation');
assert.match(emptyFieldError.metadataFieldName, 'author');
assert.match(emptyFieldError.parentMetadataFieldName, undefined);
const fieldValidationError = error as FieldValidationError;
assert.match(fieldValidationError.citationBlockName, 'citation');
assert.match(fieldValidationError.metadataFieldName, 'author');
assert.match(fieldValidationError.parentMetadataFieldName, undefined);
assert.match(
emptyFieldError.message,
fieldValidationError.message,
'There was an error when validating the field author from metadata block citation. Reason was: Expecting an array of values.',
);
});
Expand Down

0 comments on commit 32ce475

Please sign in to comment.