import * as SUT from '../index' ; import { Browser , chromium } from 'playwright' ; describe ( 'get today date of page' , ( ) : void => { let browser = undefined ; beforeEach ( ( ) : void => { } ) ; afterEach ( async ( ) : Promise < void > => { if ( browser ) { await browser . close ( ) ; } } ) ; test ( 'should return today - chromium' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( ) ; const page = await browserContext . newPage ( ) ; const result = await SUT . getToday ( page ) ; expect ( result ) . toBeDefined ( ) ; } ) ; test ( 'should return today as yyy-mm-dd - chromium' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( ) ; const page = await browserContext . newPage ( ) ; const result = await SUT . getToday ( page , 'yyyy-mm-dd' ) ; expect ( result ) . toBeDefined ( ) ; expect ( result ) . toMatch ( /^\d{4}-\d{2}-\d{2}$/i ) ; } ) ; test ( 'should return today in short format - chromium' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( ) ; const page = await browserContext . newPage ( ) ; const result = await SUT . getToday ( page , { locale : 'en' , intlOptions : { year : 'numeric' , month : 'short' , day : '2-digit' } , } ) ; const result2 = await SUT . getToday ( page , 'Jun 01, 2021' ) ; expect ( result ) . toBe ( result2 ) ; } ) ; test ( 'should return today as "Apr 15, 2021" - chromium' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( ) ; const page = await browserContext . newPage ( ) ; const result = await SUT . getToday ( page , 'Jun 01, 2021' ) ; expect ( result ) . toBeDefined ( ) ; expect ( result ) . toMatch ( /^[A-Z][a-z]{2}\s\d{2},\s\d{4}$/i ) ; } ) ; test ( 'should return an error when option is wrong' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( ) ; const page = await browserContext . newPage ( ) ; const badIntlOptions = ( < unknown > { day : 'foobar' , } ) as Intl . DateTimeFormatOptions ; const result = await SUT . getToday ( page , { locale : 'en' , intlOptions : badIntlOptions } ) ; expect ( result ) . toBe ( 'RangeError: Value foobar out of range for Intl.DateTimeFormat options property day' ) ; } ) ; } ) ;