Skip to content

Commit ecb6109

Browse files
committed
Factorize code
1 parent 3f4f4c0 commit ecb6109

File tree

1 file changed

+61
-82
lines changed

1 file changed

+61
-82
lines changed

src/archivist/index.test.js

Lines changed: 61 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -253,46 +253,59 @@ describe('Archivist', function () {
253253
});
254254

255255
describe('with combined source documents', () => {
256-
const SERVICE_ID = 'service_with_multiple_source_documents_terms';
257-
const TERMS_TYPE = 'Community Guidelines';
258-
const MOCK_CONTENT_1 = '<html><body id="main"><h1>Community Standards</h1><p>Community Standards content</p></body></html>';
259-
const MOCK_CONTENT_2 = '<html><body><p>Hate speech content</p><div id="footer">Footer</div></body></html>';
260-
const MOCK_CONTENT_3 = '<html><body><p>Violence incitement content</p><button class="share">Share</button><button class="print">Print</button></body></html>';
261-
const MOCK_CONTENT_4 = '<html><body><p>New additional policy</p></body></html>';
256+
const MULTI_SOURCE_DOCS = {
257+
SERVICE_ID: 'service_with_multiple_source_documents_terms',
258+
TERMS_TYPE: 'Community Guidelines',
259+
BASE_URL: 'https://www.service-with-multiple-source-documents-terms.example',
260+
CONTENT: {
261+
COMMUNITY_STANDARDS: '<html><body id="main"><h1>Community Standards</h1><p>Community Standards content</p></body></html>',
262+
HATE_SPEECH: '<html><body><p>Hate speech content</p><div id="footer">Footer</div></body></html>',
263+
VIOLENCE_INCITEMENT: '<html><body><p>Violence incitement content</p><button class="share">Share</button><button class="print">Print</button></body></html>',
264+
NEW_POLICY: '<html><body><p>New additional policy</p></body></html>',
265+
},
266+
PATHS: {
267+
COMMUNITY_STANDARDS: '/community-standards',
268+
HATE_SPEECH: '/community-standards/hate-speech/',
269+
VIOLENCE_INCITEMENT: '/community-standards/violence-incitement/',
270+
NEW_POLICY: '/community-standards/new-policy/',
271+
},
272+
EXPECTED_TEXTS: {
273+
COMMUNITY_STANDARDS: 'Community Standards',
274+
HATE_SPEECH: 'Hate speech content',
275+
VIOLENCE_INCITEMENT: 'Violence incitement content',
276+
NEW_POLICY: 'New additional policy',
277+
},
278+
};
279+
280+
const { SERVICE_ID, TERMS_TYPE } = MULTI_SOURCE_DOCS;
281+
282+
function setupNockForMultiSourceDocs(pathKeys) {
283+
pathKeys.forEach(pathKey => {
284+
nock(MULTI_SOURCE_DOCS.BASE_URL)
285+
.persist()
286+
.get(MULTI_SOURCE_DOCS.PATHS[pathKey])
287+
.reply(200, MULTI_SOURCE_DOCS.CONTENT[pathKey], { 'Content-Type': 'text/html' });
288+
});
289+
}
290+
291+
function disableClientScriptsForTerms(terms) {
292+
terms.sourceDocuments.forEach(doc => {
293+
doc.executeClientScripts = false;
294+
});
295+
}
262296

263297
context('when a source document is added to existing combined terms', () => {
264298
let initialVersion;
265299
let upgradeVersion;
266300

267301
before(async () => {
268-
// Mock all source documents
269-
nock('https://www.service-with-multiple-source-documents-terms.example')
270-
.persist()
271-
.get('/community-standards')
272-
.reply(200, MOCK_CONTENT_1, { 'Content-Type': 'text/html' });
273-
274-
nock('https://www.service-with-multiple-source-documents-terms.example')
275-
.persist()
276-
.get('/community-standards/hate-speech/')
277-
.reply(200, MOCK_CONTENT_2, { 'Content-Type': 'text/html' });
278-
279-
nock('https://www.service-with-multiple-source-documents-terms.example')
280-
.persist()
281-
.get('/community-standards/violence-incitement/')
282-
.reply(200, MOCK_CONTENT_3, { 'Content-Type': 'text/html' });
283-
284-
nock('https://www.service-with-multiple-source-documents-terms.example')
285-
.persist()
286-
.get('/community-standards/new-policy/')
287-
.reply(200, MOCK_CONTENT_4, { 'Content-Type': 'text/html' });
302+
setupNockForMultiSourceDocs([ 'COMMUNITY_STANDARDS', 'HATE_SPEECH', 'VIOLENCE_INCITEMENT', 'NEW_POLICY' ]);
288303

289304
app = await createAndInitializeArchivist();
290305

291306
let terms = app.services[SERVICE_ID].getTerms({ type: TERMS_TYPE });
292307

293-
terms.sourceDocuments.forEach(doc => {
294-
doc.executeClientScripts = false; // Disable executeClientScripts for testing since nock doesn't work with headless browser
295-
});
308+
disableClientScriptsForTerms(terms);
296309

297310
// First, track the terms normally to create initial version
298311
await app.track({ services: [SERVICE_ID], types: [TERMS_TYPE] });
@@ -303,7 +316,7 @@ describe('Archivist', function () {
303316

304317
terms.sourceDocuments.push(new SourceDocument({
305318
id: 'new-policy',
306-
location: 'https://www.service-with-multiple-source-documents-terms.example/community-standards/new-policy/',
319+
location: `${MULTI_SOURCE_DOCS.BASE_URL}${MULTI_SOURCE_DOCS.PATHS.NEW_POLICY}`,
307320
contentSelectors: 'body',
308321
executeClientScripts: false,
309322
filters: [],
@@ -330,16 +343,16 @@ describe('Archivist', function () {
330343
it('fetches and includes the new source document in the version', async () => {
331344
const versionContent = await upgradeVersion.content;
332345

333-
expect(versionContent).to.include('New additional policy');
346+
expect(versionContent).to.include(MULTI_SOURCE_DOCS.EXPECTED_TEXTS.NEW_POLICY);
334347
});
335348

336349
it('includes all source documents in version', async () => {
337350
const versionContent = await upgradeVersion.content;
338351

339-
expect(versionContent).to.include('Community Standards');
340-
expect(versionContent).to.include('Hate speech content');
341-
expect(versionContent).to.include('Violence incitement content');
342-
expect(versionContent).to.include('New additional policy');
352+
expect(versionContent).to.include(MULTI_SOURCE_DOCS.EXPECTED_TEXTS.COMMUNITY_STANDARDS);
353+
expect(versionContent).to.include(MULTI_SOURCE_DOCS.EXPECTED_TEXTS.HATE_SPEECH);
354+
expect(versionContent).to.include(MULTI_SOURCE_DOCS.EXPECTED_TEXTS.VIOLENCE_INCITEMENT);
355+
expect(versionContent).to.include(MULTI_SOURCE_DOCS.EXPECTED_TEXTS.NEW_POLICY);
343356
});
344357
});
345358

@@ -349,45 +362,28 @@ describe('Archivist', function () {
349362
let newLocationScope;
350363

351364
before(async () => {
352-
// Mock all source documents
353-
nock('https://www.service-with-multiple-source-documents-terms.example')
354-
.persist()
355-
.get('/community-standards')
356-
.reply(200, MOCK_CONTENT_1, { 'Content-Type': 'text/html' });
357-
358-
nock('https://www.service-with-multiple-source-documents-terms.example')
359-
.persist()
360-
.get('/community-standards/hate-speech/')
361-
.reply(200, MOCK_CONTENT_2, { 'Content-Type': 'text/html' });
362-
363-
nock('https://www.service-with-multiple-source-documents-terms.example')
364-
.persist()
365-
.get('/community-standards/violence-incitement/')
366-
.reply(200, MOCK_CONTENT_3, { 'Content-Type': 'text/html' });
365+
setupNockForMultiSourceDocs([ 'COMMUNITY_STANDARDS', 'HATE_SPEECH', 'VIOLENCE_INCITEMENT' ]);
367366

368367
app = await createAndInitializeArchivist();
369368

370-
// Disable executeClientScripts for testing since nock doesn't work with headless browser
371369
let terms = app.services[SERVICE_ID].getTerms({ type: TERMS_TYPE });
372370

373-
terms.sourceDocuments.forEach(doc => {
374-
doc.executeClientScripts = false;
375-
});
371+
disableClientScriptsForTerms(terms);
376372

377373
// First, track the terms normally
378374
await app.track({ services: [SERVICE_ID], types: [TERMS_TYPE] });
379375
initialVersion = await app.recorder.versionsRepository.findLatest(SERVICE_ID, TERMS_TYPE);
380376

381377
// Mock new location (but it won't be fetched during technical upgrade)
382-
newLocationScope = nock('https://www.service-with-multiple-source-documents-terms.example')
378+
newLocationScope = nock(MULTI_SOURCE_DOCS.BASE_URL)
383379
.persist()
384380
.get('/community-standards/hate-speech-updated/')
385381
.reply(200, '<html><body><p>Updated hate speech policy</p></body></html>', { 'Content-Type': 'text/html' });
386382

387383
// Modify the declaration to change location
388384
terms = app.services[SERVICE_ID].getTerms({ type: TERMS_TYPE });
389385

390-
terms.sourceDocuments[1].location = 'https://www.service-with-multiple-source-documents-terms.example/community-standards/hate-speech-updated/';
386+
terms.sourceDocuments[1].location = `${MULTI_SOURCE_DOCS.BASE_URL}/community-standards/hate-speech-updated/`;
391387

392388
// Apply technical upgrades
393389
await app.applyTechnicalUpgrades({ services: [SERVICE_ID], types: [TERMS_TYPE] });
@@ -421,30 +417,13 @@ describe('Archivist', function () {
421417
let upgradeVersionContent;
422418

423419
before(async () => {
424-
// Mock all source documents
425-
nock('https://www.service-with-multiple-source-documents-terms.example')
426-
.persist()
427-
.get('/community-standards')
428-
.reply(200, MOCK_CONTENT_1, { 'Content-Type': 'text/html' });
429-
430-
nock('https://www.service-with-multiple-source-documents-terms.example')
431-
.persist()
432-
.get('/community-standards/hate-speech/')
433-
.reply(200, MOCK_CONTENT_2, { 'Content-Type': 'text/html' });
434-
435-
nock('https://www.service-with-multiple-source-documents-terms.example')
436-
.persist()
437-
.get('/community-standards/violence-incitement/')
438-
.reply(200, MOCK_CONTENT_3, { 'Content-Type': 'text/html' });
420+
setupNockForMultiSourceDocs([ 'COMMUNITY_STANDARDS', 'HATE_SPEECH', 'VIOLENCE_INCITEMENT' ]);
439421

440422
app = await createAndInitializeArchivist();
441423

442-
// Disable executeClientScripts for testing since nock doesn't work with headless browser
443424
let terms = app.services[SERVICE_ID].getTerms({ type: TERMS_TYPE });
444425

445-
terms.sourceDocuments.forEach(doc => {
446-
doc.executeClientScripts = false;
447-
});
426+
disableClientScriptsForTerms(terms);
448427

449428
// First, track the terms normally
450429
await app.track({ services: [SERVICE_ID], types: [TERMS_TYPE] });
@@ -478,10 +457,10 @@ describe('Archivist', function () {
478457

479458
it('extracts content with the new selector from existing snapshot', () => {
480459
// With new selector 'h1', should only extract the heading
481-
expect(upgradeVersionContent).to.include('Community Standards');
460+
expect(upgradeVersionContent).to.include(MULTI_SOURCE_DOCS.EXPECTED_TEXTS.COMMUNITY_STANDARDS);
482461
// The rest should be from other source documents
483-
expect(upgradeVersionContent).to.include('Hate speech content');
484-
expect(upgradeVersionContent).to.include('Violence incitement content');
462+
expect(upgradeVersionContent).to.include(MULTI_SOURCE_DOCS.EXPECTED_TEXTS.HATE_SPEECH);
463+
expect(upgradeVersionContent).to.include(MULTI_SOURCE_DOCS.EXPECTED_TEXTS.VIOLENCE_INCITEMENT);
485464
});
486465

487466
it('regenerates version with updated extraction logic', () => {
@@ -493,9 +472,9 @@ describe('Archivist', function () {
493472
let newSourceScope;
494473

495474
before(async () => {
496-
newSourceScope = nock('https://www.service-with-multiple-source-documents-terms.example')
497-
.get('/community-standards/new-policy/')
498-
.reply(200, MOCK_CONTENT_4, { 'Content-Type': 'text/html' });
475+
newSourceScope = nock(MULTI_SOURCE_DOCS.BASE_URL)
476+
.get(MULTI_SOURCE_DOCS.PATHS.NEW_POLICY)
477+
.reply(200, MULTI_SOURCE_DOCS.CONTENT.NEW_POLICY, { 'Content-Type': 'text/html' });
499478

500479
app = await createAndInitializeArchivist();
501480

@@ -504,7 +483,7 @@ describe('Archivist', function () {
504483

505484
terms.sourceDocuments.push({
506485
id: 'new-policy',
507-
location: 'https://www.service-with-multiple-source-documents-terms.example/community-standards/new-policy/',
486+
location: `${MULTI_SOURCE_DOCS.BASE_URL}${MULTI_SOURCE_DOCS.PATHS.NEW_POLICY}`,
508487
contentSelectors: 'body',
509488
executeClientScripts: false,
510489
filters: [],

0 commit comments

Comments
 (0)