Commit bfe5d42b authored by Robert Knight's avatar Robert Knight

Refactor `getMetadata` tests

 - Convert promise chains to async/await

 - Add missing tests to check that the PDF URL and fingerprint URL
   appear in the object returned by `getMetadata`. These were previously
   tested indirectly in other tests.

 - Change tests to only check the specific properties of the returned
   object that are of interest. This reduces the changes needed when
   tests change unrelated parts of the output.
parent 1b539b87
...@@ -208,7 +208,6 @@ describe('PDFMetadata', function () { ...@@ -208,7 +208,6 @@ describe('PDFMetadata', function () {
}); });
}); });
describe('metadata sources', function () {
const testMetadata = { const testMetadata = {
fingerprint: 'fakeFingerprint', fingerprint: 'fakeFingerprint',
title: 'fakeTitle', title: 'fakeTitle',
...@@ -262,69 +261,58 @@ describe('PDFMetadata', function () { ...@@ -262,69 +261,58 @@ describe('PDFMetadata', function () {
}); });
}); });
describe('#getMetadata', function () { describe('#getMetadata', () => {
it('gets the title from the dc:title field', function () { it('returns the document fingerprint in the `documentFingerprint` property', async () => {
const { fakePDFViewerApplication, pdfMetadata } = createPDFMetadata(); const { pdfMetadata } = createPDFMetadata();
const expectedMetadata = { const metadata = await pdfMetadata.getMetadata();
title: 'dcFakeTitle', assert.equal(metadata.documentFingerprint, testMetadata.fingerprint);
link: [ });
{
href:
'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint,
},
{ href: fakePDFViewerApplication.url },
],
documentFingerprint: fakePDFViewerApplication.pdfDocument.fingerprint,
};
return pdfMetadata.getMetadata().then(function (actualMetadata) { it('returns the PDF URL in the `links` array', async () => {
assert.deepEqual(actualMetadata, expectedMetadata); const { pdfMetadata } = createPDFMetadata();
const metadata = await pdfMetadata.getMetadata();
assert.deepInclude(metadata.link, {
href: testMetadata.url,
}); });
}); });
it('gets the title from the documentInfo.Title field', function () { it('returns the document fingerprint in the `links` array', async () => {
const { fakePDFViewerApplication, pdfMetadata } = createPDFMetadata(); const { pdfMetadata } = createPDFMetadata();
const expectedMetadata = { const metadata = await pdfMetadata.getMetadata();
title: fakePDFViewerApplication.documentInfo.Title, assert.deepInclude(metadata.link, {
link: [ href: `urn:x-pdf:${testMetadata.fingerprint}`,
{
href:
'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint,
},
{ href: fakePDFViewerApplication.url },
],
documentFingerprint: fakePDFViewerApplication.pdfDocument.fingerprint,
};
fakePDFViewerApplication.metadata.has = sinon.stub().returns(false);
return pdfMetadata.getMetadata().then(function (actualMetadata) {
assert.deepEqual(actualMetadata, expectedMetadata);
}); });
}); });
it('does not save file:// URLs in document metadata', function () { it('does not return file:// URLs in `links` array', async () => {
const { fakePDFViewerApplication, pdfMetadata } = createPDFMetadata({ const { pdfMetadata } = createPDFMetadata({
fingerprint: 'fakeFingerprint', fingerprint: 'fakeFingerprint',
url: 'file://fake.pdf', url: 'file://fake.pdf',
}); });
const expectedMetadata = {
link: [
{
href:
'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint,
},
],
};
return pdfMetadata.getMetadata().then(function (actualMetadata) { const metadata = await pdfMetadata.getMetadata();
assert.equal(actualMetadata.link.length, 1);
assert.equal( const fileLink = metadata.link.find(link =>
actualMetadata.link[0].href, link.href.includes('file://')
expectedMetadata.link[0].href
); );
assert.isUndefined(fileLink);
}); });
it('gets the title from the dc:title field', async () => {
const { pdfMetadata } = createPDFMetadata();
const metadata = await pdfMetadata.getMetadata();
assert.equal(metadata.title, testMetadata.metadata['dc:title']);
}); });
it('gets the title from the documentInfo.Title field', async () => {
const { pdfMetadata } = createPDFMetadata({
title: 'Some title',
url: 'http://fake.com/',
});
const metadata = await pdfMetadata.getMetadata();
assert.equal(metadata.title, 'Some title');
}); });
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment