[go: nahoru, domu]

Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Jun 27, 2019
1 parent a421f82 commit e447753
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 42 deletions.
9 changes: 7 additions & 2 deletions lighthouse-core/lib/i18n/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ const _ICUMsgNotFoundMsg = 'ICU message not found in destination locale';
*/
function _formatIcuMessage(locale, icuMessageId, fallbackMessage, values) {
const localeMessages = LOCALES[locale];
if (!localeMessages) throw new Error(`Unsupported locale '${locale}'`);

const localeMessage = localeMessages[icuMessageId] && localeMessages[icuMessageId].message;
// fallback to the original english message if we couldn't find a message in the specified locale
// better to have an english message than no message at all, in some number cases it won't even matter
Expand Down Expand Up @@ -220,13 +222,16 @@ function _formatPathAsString(pathInLHR) {
* @return {LH.I18NRendererStrings}
*/
function getRendererFormattedStrings(locale) {
const icuMessageIds = Object.keys(LOCALES[locale]).filter(f => f.includes('core/report/html/'));
const localeMessages = LOCALES[locale];
if (!localeMessages) throw new Error(`Unsupported locale '${locale}'`);

const icuMessageIds = Object.keys(localeMessages).filter(f => f.includes('core/report/html/'));
/** @type {LH.I18NRendererStrings} */
const strings = {};
for (const icuMessageId of icuMessageIds) {
const [filename, varName] = icuMessageId.split(' | ');
if (!filename.endsWith('util.js')) throw new Error(`Unexpected message: ${icuMessageId}`);
strings[varName] = LOCALES[locale][icuMessageId].message;
strings[varName] = localeMessages[icuMessageId].message;
}

return strings;
Expand Down
72 changes: 32 additions & 40 deletions lighthouse-core/test/gather/gather-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ describe('GatherRunner', function() {
assert.equal(tests.calledCleanBrowserCaches, true);
});

it('returns a pageLoadError and no artifacts with network errors', async () => {
it('returns a pageLoadError and no artifacts when there is a network error', async () => {
const requestedUrl = 'https://example.com';
// This page load error should be overriden by NO_DOCUMENT_REQUEST (for being
// more specific) since requestedUrl does not match any network request in fixture.
Expand All @@ -434,31 +434,27 @@ describe('GatherRunner', function() {
gotoURL: url => url.includes('blank') ? null : Promise.reject(navigationError),
});

const passConfig = {
gatherers: [
{instance: new TestGatherer()},
],
};

const settings = {};

const passContext = {
url: requestedUrl,
const config = new Config({
passes: [{
recordTrace: true,
passName: 'firstPass',
gatherers: [{instance: new TestGatherer()}],
}],
});
const options = {
driver,
passConfig,
settings,
LighthouseRunWarnings: [],
baseArtifacts: await GatherRunner.initializeBaseArtifacts({driver, settings, requestedUrl}),
requestedUrl,
settings: config.settings,
};

const {artifacts, pageLoadError} = await GatherRunner.runPass(passContext);
expect(passContext.LighthouseRunWarnings).toHaveLength(1);
expect(pageLoadError).toBeInstanceOf(Error);
expect(pageLoadError.code).toEqual('NO_DOCUMENT_REQUEST');
expect(artifacts).toEqual({});
const artifacts = await GatherRunner.run(config.passes, options);
expect(artifacts.LighthouseRunWarnings).toHaveLength(1);
expect(artifacts.PageLoadError).toBeInstanceOf(Error);
expect(artifacts.PageLoadError.code).toEqual('NO_DOCUMENT_REQUEST');
expect(artifacts.TestGatherer).toBeUndefined();
});

it('returns a pageLoadError and no artifacts with navigation errors', async () => {
it('returns a pageLoadError and no artifacts when there is a navigation error', async () => {
const requestedUrl = 'https://example.com';
// This time, NO_FCP should win because it's the only error left.
const navigationError = new LHError(LHError.errors.NO_FCP);
Expand All @@ -470,28 +466,24 @@ describe('GatherRunner', function() {
},
});

const passConfig = {
gatherers: [
{instance: new TestGatherer()},
],
};

const settings = {};

const passContext = {
url: requestedUrl,
const config = new Config({
passes: [{
recordTrace: true,
passName: 'firstPass',
gatherers: [{instance: new TestGatherer()}],
}],
});
const options = {
driver,
passConfig,
settings,
LighthouseRunWarnings: [],
baseArtifacts: await GatherRunner.initializeBaseArtifacts({driver, settings, requestedUrl}),
requestedUrl,
settings: config.settings,
};

const {artifacts, pageLoadError} = await GatherRunner.runPass(passContext);
expect(passContext.LighthouseRunWarnings).toHaveLength(1);
expect(pageLoadError).toBeInstanceOf(Error);
expect(pageLoadError.code).toEqual('NO_FCP');
expect(artifacts).toEqual({});
const artifacts = await GatherRunner.run(config.passes, options);
expect(artifacts.LighthouseRunWarnings).toHaveLength(1);
expect(artifacts.PageLoadError).toBeInstanceOf(Error);
expect(artifacts.PageLoadError.code).toEqual('NO_FCP');
expect(artifacts.TestGatherer).toBeUndefined();
});

it('does not clear origin storage with flag --disable-storage-reset', () => {
Expand Down
16 changes: 16 additions & 0 deletions lighthouse-core/test/lib/i18n/i18n-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ describe('i18n', () => {
expect(strings.passedAuditsGroupTitle).toEqual('[Þåššéð åûðîţš one two]');
expect(strings.snippetCollapseButtonLabel).toEqual('[Çöļļåþšé šñîþþéţ one two]');
});

it('throws an error for invalid locales', () => {
expect(_ => i18n.getRendererFormattedStrings('not-a-locale'))
.toThrow(`Unsupported locale 'not-a-locale'`);
});
});

describe('#getFormatted', () => {
it('throws an error for invalid locales', () => {
// Populate a string to try to localize to a bad locale.
const UIStrings = {testMessage: 'testy test'};
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);

expect(_ => i18n.getFormatted(str_(UIStrings.testMessage), 'still-not-a-locale'))
.toThrow(`Unsupported locale 'still-not-a-locale'`);
});
});

describe('#lookupLocale', () => {
Expand Down

0 comments on commit e447753

Please sign in to comment.