Skip to content

Commit

Permalink
refactor(common): minor NgOptimizedImage directive updates (#47082)
Browse files Browse the repository at this point in the history
This commit contains a set of small changes based on the feedback received in #47082.

PR Close #47082
  • Loading branch information
AndrewKushnir authored and Pawel Kozlowski committed Aug 16, 2022
1 parent f81765b commit 0ca5eb3
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export class LCPImageObserver implements OnDestroy {
// a page, by virtue of being the only thing on the page so far, is often a LCP candidate
// and gets reported by PerformanceObserver, but isn't necessarily the LCP element.
const lcpElement = entries[entries.length - 1];
// Cast to `any` due to missing `element` on observed type of entry.

// Cast to `any` due to missing `element` on the `LargestContentfulPaint` type of entry.
// See https://developer.mozilla.org/en-US/docs/Web/API/LargestContentfulPaint
const imgSrc = (lcpElement as any).element?.src ?? '';

// Exclude `data:` and `blob:` URLs, since they are not supported by the directive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
// Monitor whether an image is an LCP element only in case
// the `priority` attribute is missing. Otherwise, an image
// has the necessary settings and no extra checks are required.
withLCPImageObserver(
invokeLCPImageObserverCallback(
this.injector,
(observer: LCPImageObserver) =>
observer.registerImage(this.getRewrittenSrc(), this.rawSrc));
Expand Down Expand Up @@ -345,7 +345,7 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
ngOnDestroy() {
if (ngDevMode) {
if (!this.priority && this._renderedSrc !== null) {
withLCPImageObserver(
invokeLCPImageObserverCallback(
this.injector,
(observer: LCPImageObserver) => observer.unregisterImage(this._renderedSrc!));
}
Expand Down Expand Up @@ -383,7 +383,7 @@ function inputToBoolean(value: unknown): boolean {
* NgZone to make sure none of the calls inside the `LCPImageObserver` class trigger unnecessary
* change detection
*/
function withLCPImageObserver(
function invokeLCPImageObserverCallback(
injector: Injector, operation: (observer: LCPImageObserver) => void): void {
const ngZone = injector.get(NgZone);
return ngZone.runOutsideAngular(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/bundling/image-directive/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ts_devserver(
ts_library(
name = "img_dir_e2e_tests_lib",
testonly = True,
srcs = ["e2e/util.ts"] + glob([
srcs = ["e2e/browser-logs-util.ts"] + glob([
"e2e/**/*.e2e-spec.ts",
]),
tsconfig = ":e2e/tsconfig-e2e.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {browser, by, element} from 'protractor';
import {logging} from 'selenium-webdriver';

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

describe('NgOptimizedImage directive', () => {
it('should render an image with an updated `src`', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import {browser, by, element, ExpectedConditions} from 'protractor';
import {logging} from 'selenium-webdriver';

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

describe('NgOptimizedImage directive', () => {
it('should not warn if there is no image distortion', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import {browser, by, element} from 'protractor';
import {logging} from 'selenium-webdriver';

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

describe('NgOptimizedImage directive', () => {
it('should log a warning when a `priority` is missing on an LCP image', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import {browser, by, element, ExpectedConditions} from 'protractor';
import {logging} from 'selenium-webdriver';

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

describe('NgOptimizedImage directive', () => {
it('should not warn if there is no oversized image', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import {browser, by, element, ElementHelper} from 'protractor';
import {logging} from 'selenium-webdriver';

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

// Verifies that both images used in a component were rendered.
async function verifyImagesPresent(element: ElementHelper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
const protractorUtils = require('@bazel/protractor/protractor-utils');
const protractor = require('protractor');

/**
* Helper function to start up a server for testing using Protractor utils.
* Used as a part of the `protractor_web_test_suite` rule configuration.
*/
module.exports = async function(config) {
const {port} = await protractorUtils.runServer(config.workspace, config.server, '-port', []);
const serverUrl = `http://localhost:${port}`;
Expand Down

0 comments on commit 0ca5eb3

Please sign in to comment.