UNPKG

@serenity-js/assertions

Version:

Serenity/JS universal assertion library supporting all types of functional tests, including both web and REST API scenarios

99 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isPresent = isPresent; const core_1 = require("@serenity-js/core"); /** * Creates an [expectation](https://serenity-js.org/api/core/class/Expectation/) that is met when the `actual` value is not undefined or null. * * Also, when the `actual` implements [`Optional`](https://serenity-js.org/api/core/interface/Optional/), * the expectation is met when calling [`Optional.isPresent`](https://serenity-js.org/api/core/interface/Optional/#isPresent) * returns an [`Answerable`](https://serenity-js.org/api/core/#Answerable) that resolves to `true` * * ## Ensuring that a value is defined * * ```ts * import { actorCalled } from '@serenity-js/core' * import { CallAnApi, Send, GetRequest, LastResponse } from '@serenity-js/rest' * import { Ensure, isPresent } from '@serenity-js/assertions' * * interface Product { * name: string; * } * * interface ProductsResponse { * products: Product[]; * } * * await actorCalled('Apisitt') * .whoCan(CallAnApi.at('https://api.example.org')) * .attemptsTo( * Send.a(GetRequest.to('/products')), * Ensure.that(LastResponse.body<ProductsResponse>().products[0], isPresent()), * ) * ``` * * ## Checking if a PageElement is present * * ```ts * import { actorCalled, Check } from '@serenity-js/core'; * import { BrowseTheWebWithPlaywright } from '@serenity-js/playwright'; * import { By, Click, Navigate, PageElement } from '@serenity-js/web'; * import { Browser, chromium } from 'playwright'; * * class NewsletterSubscription { * static modal = () => * PageElement.located(By.id('newsletter-subscription')) * .describedAs('newsletter subscription modal') * * static closeButton = () => * PageElement.located(By.class('.close')) * .of(NewsletterSubscription.modal()) * .describedAs('close button') * } * * const browser = await chromium.launch({ headless: true }); * * await actorCalled('Isabela') * .whoCan(BrowseTheWebWithPlaywright.using(browser)) * .attemptsTo( * Navigate.to(`https://example.org`), * Check.whether(NewsletterSubscription.modal(), isPresent()) * .andIfSo(Click.on(NewsletterSubscription.closeButton())), * ) * ``` * * @group Expectations */ function isPresent() { return new IsPresent(); } class IsPresent extends core_1.Expectation { static isOptional(value) { return value !== undefined && value !== null && typeof value.isPresent === 'function'; } static valueToCheck(actual, actor) { if (IsPresent.isOptional(actual)) { return actual; } return actor.answer(actual); } static async isPresent(value, actor) { if (IsPresent.isOptional(value)) { return actor.answer(value.isPresent()); } return value !== undefined && value !== null; } constructor() { super('isPresent', 'become present', async (actor, actual) => { const value = await IsPresent.valueToCheck(actual, actor); const result = await IsPresent.isPresent(value, actor); return result ? new core_1.ExpectationMet('become present', core_1.ExpectationDetails.of('isPresent'), true, actual) : new core_1.ExpectationNotMet('become present', core_1.ExpectationDetails.of('isPresent'), true, actual); }); } } //# sourceMappingURL=isPresent.js.map