Skip to content

Commit

Permalink
test(common): add e2e tests for the fill mode checks in the NgOptimiz…
Browse files Browse the repository at this point in the history
…edImage directive (angular#48036)

This commit adds extra e2e tests for the fill mode checks in the NgOptimizedImage directive to make
sure a warning is logged in a console.

PR Close angular#48036
  • Loading branch information
AndrewKushnir authored and trekladyone committed Feb 1, 2023
1 parent ff4a457 commit e962842
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -877,8 +877,9 @@ function assertNonZeroRenderedHeight(
RuntimeErrorCode.INVALID_INPUT,
`${imgDirectiveDetails(dir.ngSrc)} the height of the fill-mode image is zero. ` +
`This is likely because the containing element does not have the CSS 'position' ` +
`property set to one of the following: "relative", "fixed", or "absolute". Make this ` +
`change to ensure that the image is visible.`));
`property set to one of the following: "relative", "fixed", or "absolute". ` +
`To fix this problem, make sure the container element has the CSS 'position' ` +
`property defined and the height of the element is not zero.`));
}
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/test/bundling/image-directive/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ng_module(
name = "image-directive",
srcs = [
"e2e/basic/basic.ts",
"e2e/fill-mode/fill-mode.ts",
"e2e/image-distortion/image-distortion.ts",
"e2e/lcp-check/lcp-check.ts",
"e2e/oversized-image/oversized-image.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

/* tslint:disable:no-console */
import {browser} from 'protractor';
import {logging} from 'selenium-webdriver';

import {collectBrowserLogs} from '../browser-logs-util';

describe('NgOptimizedImage directive', () => {
it('should not warn when an image in the fill mode is rendered correctly', async () => {
await browser.get('/e2e/fill-mode-passing');
const logs = await collectBrowserLogs(logging.Level.WARNING);
expect(logs.length).toEqual(0);
});

it('should warn if an image in the fill mode has zero height after rendering', async () => {
await browser.get('/e2e/fill-mode-failing');
const logs = await collectBrowserLogs(logging.Level.WARNING);

expect(logs.length).toEqual(1);
// Image loading order is not guaranteed, so all logs, rather than single entry
// needs to be checked in order to test whether a given error message is present.
const expectErrorMessageInLogs = (logs: logging.Entry[], message: string) => {
expect(logs.some((log) => {
return log.message.includes(message);
})).toBeTruthy();
};

expectErrorMessageInLogs(
logs,
'NG02952: The NgOptimizedImage directive (activated on an \\u003Cimg> element ' +
'with the `ngSrc=\\"/e2e/logo-500w.jpg\\"`) has detected that the height ' +
'of the fill-mode image is zero. This is likely because the containing element ' +
'does not have the CSS \'position\' property set to one of the following: ' +
'\\"relative\\", \\"fixed\\", or \\"absolute\\". To fix this problem, ' +
'make sure the container element has the CSS \'position\' ' +
'property defined and the height of the element is not zero.');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {NgOptimizedImage} from '@angular/common';
import {Component} from '@angular/core';

@Component({
selector: 'fill-mode-passing',
standalone: true,
imports: [NgOptimizedImage],
template: `
<!-- Make sure an image in the fill mode has the size of a container -->
<div style="position: absolute; width: 100px; height: 100px;">
<img ngSrc="/e2e/logo-500w.jpg" fill priority>
</div>
`,
})
export class FillModePassingComponent {
}
@Component({
selector: 'fill-mode-failing',
standalone: true,
imports: [NgOptimizedImage],
template: `
<div style="position: relative; width: 100%;">
<img ngSrc="/e2e/logo-500w.jpg" fill priority>
</div>
`,
})
export class FillModeFailingComponent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

/* tslint:disable:no-console */
import {browser, by, element, ExpectedConditions} from 'protractor';
import {browser} from 'protractor';
import {logging} from 'selenium-webdriver';

import {collectBrowserLogs} from '../browser-logs-util';
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/bundling/image-directive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {bootstrapApplication, provideProtractorTestingSupport} from '@angular/pl
import {RouterModule} from '@angular/router';

import {BasicComponent} from './e2e/basic/basic';
import {FillModeFailingComponent, FillModePassingComponent} from './e2e/fill-mode/fill-mode';
import {ImageDistortionFailingComponent, ImageDistortionPassingComponent} from './e2e/image-distortion/image-distortion';
import {LcpCheckComponent} from './e2e/lcp-check/lcp-check';
import {OversizedImageComponentFailing, OversizedImageComponentPassing} from './e2e/oversized-image/oversized-image';
Expand Down Expand Up @@ -38,6 +39,8 @@ const ROUTES = [
{path: 'e2e/image-distortion-failing', component: ImageDistortionFailingComponent},
{path: 'e2e/oversized-image-passing', component: OversizedImageComponentPassing},
{path: 'e2e/oversized-image-failing', component: OversizedImageComponentFailing},
{path: 'e2e/fill-mode-passing', component: FillModePassingComponent},
{path: 'e2e/fill-mode-failing', component: FillModeFailingComponent},
];

bootstrapApplication(RootComponent, {
Expand Down

0 comments on commit e962842

Please sign in to comment.