Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
paweltomaszewskisaucelabs committed Sep 25, 2024
1 parent 4160015 commit 445c9c4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
} from '@saucelabs/visual';
import { getMetaInfo, getVisualApi } from '../../utils/api';
import { VISUAL_BUILD_ID_KEY } from '../../utils/constants';
import { NightwatchAPI, NightwatchCustomCommandsModel } from 'nightwatch';
import {
NightwatchAPI,
NightwatchCustomCommandsModel,
ScopedElement,
} from 'nightwatch';
import { CheckOptions, NightwatchIgnorable, RunnerSettings } from '../../types';
import type { Runnable } from 'mocha';

Expand Down Expand Up @@ -148,7 +152,11 @@ class SauceVisualCheck implements NightwatchCustomCommandsModel {
sessionMetadata: metaInfo,
suiteName,
testName,
fullPageConfig: await getFullPageConfig(fullPage, options.fullPage),
fullPageConfig: await getFullPageConfig<ScopedElement>(
fullPage,
options.fullPage,
async (el) => await el.getId(),
),
clipElement:
(await options.clipElement?.getId()) ?? clipElementFromClipSelector,
captureDom: options.captureDom ?? globalCaptureDom,
Expand Down
2 changes: 1 addition & 1 deletion visual-js/visual-nightwatch/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface CheckOptions {
ignore?: NightwatchIgnorable[];
regions?: RegionType<ElementType>[];
diffingMethod?: DiffingMethod;
fullPage?: FullPageScreenshotOptions;
fullPage?: FullPageScreenshotOptions<ScopedElement>;
/**
* Whether we should take a snapshot of the DOM to compare with as a part of the diffing process.
*/
Expand Down
16 changes: 11 additions & 5 deletions visual-js/visual-wdio/src/SauceVisualService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import {

import logger from '@wdio/logger';
import chalk from 'chalk';
import { Ignorable, isWdioElement, WdioElement } from './guarded-types.js';
import {
FullPageScreenshotWdioOptions,
Ignorable,
isWdioElement,
WdioElement,
} from './guarded-types.js';
import { backOff } from 'exponential-backoff';
import type { Test } from '@wdio/types/build/Frameworks';

Expand Down Expand Up @@ -95,7 +100,7 @@ export type SauceVisualServiceOptions = {
clipSelector?: string;
clipElement?: WdioElement;
region?: SauceRegion;
fullPage?: FullPageScreenshotOptions;
fullPage?: FullPageScreenshotWdioOptions;
baselineOverride?: BaselineOverrideIn;
};

Expand Down Expand Up @@ -131,7 +136,7 @@ export type CheckOptions = {
captureDom?: boolean;
diffingMethod?: DiffingMethod;
disable?: (keyof DiffingOptionsIn)[];
fullPage?: FullPageScreenshotOptions;
fullPage?: FullPageScreenshotWdioOptions;
baselineOverride?: BaselineOverrideIn;
};

Expand Down Expand Up @@ -165,7 +170,7 @@ export default class SauceVisualService implements Services.ServiceInstance {
captureDom: boolean | undefined;
clipSelector: string | undefined;
clipElement: WdioElement | undefined;
fullPage?: FullPageScreenshotOptions;
fullPage?: FullPageScreenshotWdioOptions;
apiClient: VisualApi;
baselineOverride?: BaselineOverrideIn;

Expand Down Expand Up @@ -442,9 +447,10 @@ export default class SauceVisualService implements Services.ServiceInstance {
options.diffingMethod || this.diffingMethod || DiffingMethod.Balanced,
suiteName: this.test?.parent,
testName: this.test?.title,
fullPageConfig: await getFullPageConfig(
fullPageConfig: await getFullPageConfig<WdioElement>(
this.fullPage,
options.fullPage,
(el) => el.elementId,
),
baselineOverride: options.baselineOverride || this.baselineOverride,
});
Expand Down
9 changes: 8 additions & 1 deletion visual-js/visual-wdio/src/guarded-types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { type } from 'arktype';
import { makeValidate, RegionIn } from '@saucelabs/visual';
import {
FullPageScreenshotOptions,
makeValidate,
RegionIn,
} from '@saucelabs/visual';

export type WdioElement = WebdriverIO.Element;

export type FullPageScreenshotWdioOptions =
FullPageScreenshotOptions<WdioElement>;

const wdioElementType = type({
elementId: 'string',
selector: 'string',
Expand Down
5 changes: 2 additions & 3 deletions visual-js/visual/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { RegionIn } from './graphql/__generated__/graphql';
import { SelectiveRegionOptions } from './common/selective-region';
import { SauceRegion } from './common/regions';
import { WdioElement } from '@saucelabs/wdio-sauce-visual-service/build/guarded-types';

export type FullPageScreenshotOptions =
export type FullPageScreenshotOptions<T> =
| boolean
| {
/**
Expand Down Expand Up @@ -38,7 +37,7 @@ export type FullPageScreenshotOptions =
/**
* Element used for scrolling (available only in native apps)
*/
scrollElement?: WdioElement | Promise<WdioElement>;
scrollElement?: T | Promise<T>;
};

export type Ignorable<T> = T | T[] | Promise<T> | Promise<T[]> | RegionIn;
Expand Down
24 changes: 12 additions & 12 deletions visual-js/visual/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ import fs from 'fs/promises';
import * as os from 'node:os';
import { SauceRegion } from './common/regions';

export const getFullPageConfig: (
main?: FullPageScreenshotOptions | boolean,
local?: FullPageScreenshotOptions | boolean,
) => Promise<FullPageConfigIn | undefined> = async (main, local) => {
export const getFullPageConfig: <T>(
main?: FullPageScreenshotOptions<T> | boolean,
local?: FullPageScreenshotOptions<T> | boolean,
getId?: (el: T) => Promise<string> | string,
) => Promise<FullPageConfigIn | undefined> = async (main, local, getId) => {
const isNoConfig = !main && !local;
const isLocalOff = local === false;

if (isNoConfig || isLocalOff) {
return;
}

const globalCfg: FullPageScreenshotOptions =
typeof main === 'object' ? main : {};
const localCfg: FullPageScreenshotOptions =
typeof local === 'object' ? local : {};
const mergedCfg = { ...globalCfg, ...localCfg };
if (mergedCfg.scrollElement) {
mergedCfg.scrollElement = (await mergedCfg.scrollElement).elementId;
const globalCfg: typeof main = typeof main === 'object' ? main : {};
const localCfg: typeof main = typeof local === 'object' ? local : {};
const { scrollElement, ...rest } = { ...globalCfg, ...localCfg };
const result: FullPageConfigIn = rest;
if (scrollElement && getId) {
result.scrollElement = await getId(await scrollElement);
}

return mergedCfg;
return result;
};

export const isSkipMode = (): boolean => {
Expand Down

0 comments on commit 445c9c4

Please sign in to comment.