cumulocity-cypress
Version:
Cypress commands for Cumulocity IoT
126 lines (125 loc) • 7.07 kB
TypeScript
import { FormatWidth } from "../locale/locale";
import * as dateFns from "date-fns";
import { Locale } from "date-fns";
export { Locale };
declare global {
namespace Cypress {
interface Chainable extends ChainableWithState {
/**
* Creates a `Date` object from given string or number input. Supported strings must be created using the Cumulocity `c8yDate` or Angular `date` pipes. Format is detected automatically for reading the strings based on locale set via `cy.setLanguage`. Supported formats are datetime, time, date.
*
* Configuration is available via `options` as optional argument.
*
* English (en) and german (de) locales are registered by default. If any other locale is required, the locale must be imported in your Cypress support file.
*
* @example
* cy.setLanguage("en");
* cy.wrap("26 May 2023, 15:59:00").toDate().then((date) => {
* // do something with date
* })
* cy.toDate("26 May 2023, 15:59:00").then((date) => {
* // do something with date
* })
*
* @see registerLocale
* @see registerDefaultLocales
* @see https://date-fns.org/v2.30.0/docs/format
*
* @param {ISODateSource} source string or number of an array of strings or numbers to convert into a date
* @param {ISODateOptions} options the configuration options for date processing
*/
toDate<T = Date | Date[]>(source?: ISODateSource, options?: ISODateOptions): Chainable<T | undefined>;
toDate<T = Date | Date[]>(options?: ISODateOptions): Chainable<T | undefined>;
/**
* Creates an ISO formatted date string from given or yielded string or number input. Use `cy.toDate()` for reading the date and converts into ISO date.
*
* @example
* cy.setLanguage("en");
* cy.wrap("26 May 2023, 15:59:00").toISODate().then((date) => {
* // do something with date
* })
* cy.toISODate("26 May 2023, 15:59:00").then((date) => {
* // do something with date
* })
*
* @see toDate
* @see registerLocale
* @see registerDefaultLocales
* @see https://date-fns.org/v2.30.0/docs/format
*
* @param {ISODateSource} source string or number of an array of strings or numbers to convert into a date
* @param {ISODateOptions} options the configuration options for date processing
*/
toISODate<T = ISODateSource>(source?: T, options?: ISODateOptions): Chainable<string | string[] | undefined>;
toISODate(options?: ISODateOptions): Chainable<string | string[] | undefined>;
/**
* Returns the Angular date format string used to format the given source string. If no format is found, `undefined` is returned.
*
* @param source date string formatted using Angular `date` or Cumulocity `c8yDate` pipe
* @param options the configuration options
*/
dateFormat(source?: string, options?: Pick<ISODateOptions, "invalid" | "language" | "log">): Chainable<string | undefined>;
dateFormat(options?: Pick<ISODateOptions, "invalid" | "language" | "log">): Chainable<string | undefined>;
/**
* Compare Cumulocity `c8yDate` or Angular `date` pipe formatted string with a `Date` object or an ISO date string as used in Cumulocity REST API.
*
* Comparing is done by formatting the `target` using the format of the source string. This way only the relevant components of the date will be compared.
*
* @example
* cy.setLanguage("en");
* cy.compareDates("25/05/2023", new Date()).should("eq", true);
* cy.compareDates("25/05/2023, 16:22", "2023-05-25T14:22:12.320Z").should("eq", true);
*
* @param source date string formatted using Angular `date` or Cumulocity `c8yDate` pipe
* @param target `Date`or ISO date string to compare with
* @param options the configuration options
*/
compareDates(source: string, target: Date | number | string, options?: Pick<ISODateOptions, "invalid" | "language" | "log">): Chainable<boolean>;
compareDates(target: Date | number | string, options?: Pick<ISODateOptions, "invalid" | "language" | "log">): Chainable<boolean>;
}
interface Cypress {
datefns: typeof dateFns;
}
}
type ISODateSource = string | string[] | number | number[];
interface ISODateOptions {
/** Override format used to read the formatted date string. */
format?: string;
/** Override language used to read the formatted date string. */
language?: string;
/** Use given FormatWith to read the formatted date string. */
formatWidth?: FormatWidth;
/** Enable or disable logging to debug console. Defaults to `true`.*/
log?: boolean;
/** How to process invalid date strings. `Keep` to just use input string as value, `throw` to throw an exception and `ignore` to just ignore in output. */
invalid?: "keep" | "ignore" | "throw";
/** If `strictFormats` is enabled, only the Angular date formats will be used. If disabled, also other ways will be tried to parse the formatted date string. */
strictFormats?: boolean;
consoleProps?: any;
logger?: Cypress.Log;
}
/**
* Register additional locale data to be used with `cy.toISODate()` and Cypress date related
* functionality. You need to provide Angular locale data and optionally the
* corresponding date-fns locale object for proper date parsing.
*
* @param data Angular locale data. Angular locale data is an array of locale specific data.
* @param localeId The locale id to use for the locale. `cy.setLanguage(localeId)` will use this locale.
* @param dfnsLocale Optional date-fns locale object to use for date parsing.
* @param extraData Optional extra data to be added to the locale data.
*
* @example
* ```typescript
* // register en-GB to be used as english locale (en-GB is Cumulocity default english locale)
* import localeEn = require("@angular/common/locales/en-GB");
* import { enGB } from "date-fns/locale";
* registerLocale(localeEn, "en", enGB);
* ```
*/
function registerLocale(c8yLocaleId: string, data: unknown[], dfnsLocale?: Locale | null, extraData?: unknown | undefined): void;
/**
* Registers default Angular locales. Currently this is `en` (en-GB) and `de` (de) Angular locales.
*/
function registerDefaultLocales(): void;
function setLocale(localeId: string): void;
}