Skip to content

Commit

Permalink
Testing grab inline js with config variables from HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-karatsiuba authored and kelson42 committed Feb 18, 2023
1 parent d6fa4a4 commit 061b04e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/util/saveArticles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export async function saveArticles(zimCreator: ZimCreator, downloader: Downloade
}
}

async function getModuleDependencies(articleId: string, mw: MediaWiki, downloader: Downloader) {
export async function getModuleDependencies(articleId: string, mw: MediaWiki, downloader: Downloader) {
/* These vars will store the list of js and css dependencies for
the article we are downloading. */
let jsConfigVars = ''
Expand Down
27 changes: 26 additions & 1 deletion test/unit/saveArticles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { startRedis, stopRedis, redisStore } from './bootstrap.js'
import domino from 'domino'

import { setupScrapeClasses, convertWikicodeToHtml, testHtmlRewritingE2e } from '../util.js'
import { saveArticles, treatMedias, applyOtherTreatments, treatSubtitle, treatVideo } from '../../src/util/saveArticles.js'
import { saveArticles, getModuleDependencies, treatMedias, applyOtherTreatments, treatSubtitle, treatVideo } from '../../src/util/saveArticles.js'
import { ZimArticle } from '@openzim/libzim'
import { Dump } from '../../src/Dump'
import { mwRetToArticleDetail, renderDesktopArticle, DELETED_ARTICLE_ERROR } from '../../src/util/index.js'
Expand Down Expand Up @@ -349,4 +349,29 @@ describe('saveArticles', () => {
// Throwing error if article is deleted
expect(() => renderDesktopArticle(articleJsonObject, 'deletedArticle', { title: 'deletedArticle' })).toThrow(new Error(DELETED_ARTICLE_ERROR))
})

test('Load inline js from HTML', async () => {
const { downloader, mw } = await setupScrapeClasses() // en wikipedia

const _moduleDependencies = await getModuleDependencies('Potato', mw, downloader)
// next variables declared to avoid "variable is not defined" errors
let RLCONF: any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let RLSTATE: any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let RLPAGEMODULES: any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const document: any = { documentElement: { className: '' }, cookie: '' }

// eslint-disable-next-line no-eval
eval(_moduleDependencies.jsConfigVars)
expect(RLCONF).toMatchObject({
wgPageName: 'Potato',
wgTitle: 'Potato',
wgPageContentLanguage: 'en',
wgPageContentModel: 'wikitext',
wgRelevantPageName: 'Potato',
wgRelevantArticleId: 23501,
})
})
})

0 comments on commit 061b04e

Please sign in to comment.