File size: 5,280 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import { describe, expect, test, jest } from '@jest/globals';
import {
getRandomInteger,
getAccountCredential,
getCalypsoURL,
getAccountSiteURL,
toTitleCase,
createSuiteTitle,
} from '../data-helper';
import { SecretsManager } from '../secrets';
import type { Secrets } from '../secrets';
const fakeSecrets = {
testAccounts: {
basicUser: {
username: 'wpcomuser2',
password: 'hunter2',
primarySite: 'wpcomuser.wordpress.com/',
},
noUrlUser: {
username: 'nourluser',
password: 'password1234',
},
},
} as unknown as Secrets;
jest.spyOn( SecretsManager, 'secrets', 'get' ).mockImplementation( () => fakeSecrets );
describe( 'DataHelper Tests', function () {
describe( 'Test: getRandomInteger', function () {
type TestCase = { min: number; max: number; expected: number[] };
test.each< TestCase >`
min | max | expected
${ 0 } | ${ 0 } | ${ [ 0 ] }
${ 0 } | ${ 1 } | ${ [ 0, 1 ] }
${ 0 } | ${ 200 } | ${ [ ...Array( 200 ).keys() ] }
`( 'Generated integer is within $min and $max ranges', function ( { min, max, expected } ) {
const generated = getRandomInteger( min, max );
expect( generated ).toBeGreaterThanOrEqual( min );
expect( expected.includes( generated ) );
} );
} );
describe( 'Test: getCalypsoURL', function () {
type Params = Parameters< typeof getCalypsoURL >;
test.each< { route: Params[ 0 ]; queryStrings: Params[ 1 ]; expected: string } >`
route | queryStrings | expected
${ '/' } | ${ undefined } | ${ 'https://wordpress.com/' }
${ 'log-in' } | ${ undefined } | ${ 'https://wordpress.com/log-in' }
${ 'post/new' } | ${ { param: 'test' } } | ${ 'https://wordpress.com/post/new?param=test' }
${ 'post/new' } | ${ { param: 'test', query: 'jest-test' } } | ${ 'https://wordpress.com/post/new?param=test&query=jest-test' }
${ 'post/new' } | ${ { param: 'ASCIIではありません' } } | ${ 'https://wordpress.com/post/new?param=ASCII%E3%81%A7%E3%81%AF%E3%81%82%E3%82%8A%E3%81%BE%E3%81%9B%E3%82%93' }
`(
'Returns $expected if getCalypsoURL is called with $route and $queryStrings',
function ( { route, queryStrings, expected } ) {
expect( getCalypsoURL( route, queryStrings ) ).toBe( expected );
}
);
} );
describe( 'Test: getAccountCredential', function () {
type AccountType = Parameters< typeof getAccountCredential >[ 0 ];
test.each< { accountType: AccountType; expected: string } >`
accountType | expected
${ 'basicUser' } | ${ { username: 'wpcomuser2', password: 'hunter2', totpKey: undefined } }
${ 'noUrlUser' } | ${ { username: 'nourluser', password: 'password1234', totpKey: undefined } }
`(
'Returns $expected if getAccountCredential is called with $accountType',
function ( { accountType, expected } ) {
expect( getAccountCredential( accountType ) ).toStrictEqual( expected );
}
);
test.each< { accountType: AccountType } >`
accountType
${ 'nonexistent_user' }
`(
'Throws error if getAccountCredential is called with $accountType',
function ( { accountType } ) {
expect( () => getAccountCredential( accountType ) ).toThrow();
}
);
} );
describe( 'Test: getAccountSiteURL', function () {
type AccountType = Parameters< typeof getAccountCredential >[ 0 ];
type TestCase = { accountType: AccountType; expected: string };
test.each< TestCase >`
accountType | expected
${ 'basicUser' } | ${ 'https://wpcomuser.wordpress.com/' }
`(
'Returns $expected if getAccountSiteURL is called with $accountType',
function ( { accountType, expected } ) {
expect( getAccountSiteURL( accountType ) ).toStrictEqual( expected );
}
);
test.each< TestCase >`
accountType | expected
${ 'nonexistent_user' } | ${ Error }
${ 'noUrlUser' } | ${ ReferenceError }
`(
'Throws error if getAccountSiteURL is called with $accountType',
function ( { accountType, expected } ) {
expect( () => getAccountSiteURL( accountType ) ).toThrow( expected );
}
);
} );
describe( 'Test: toTitleCase', function () {
test.each< { words: string; expected: string } >`
words | expected
${ 'test' } | ${ 'Test' }
${ 'test words' } | ${ 'Test Words' }
${ [ 'test', 'words' ] } | ${ 'Test Words' }
${ 'Test Words Third' } | ${ 'Test Words Third' }
${ '11 Words Third' } | ${ '11 Words Third' }
${ [ '12', '33', 'ABCDE' ] } | ${ '12 33 ABCDE' }
`( 'Returns $expected if toTitleCase is called with $words', function ( { words, expected } ) {
expect( toTitleCase( words ) ).toStrictEqual( expected );
} );
} );
describe( 'Test: createSuiteTitle', function () {
test.each`
suite | viewport | expected
${ 'Feature (Click: Tap)' } | ${ 'desktop' } | ${ 'Feature (Click: Tap)' }
${ 'Manage' } | ${ 'mobile' } | ${ 'Manage' }
`(
'Returns $expected if toTitleCase is called with $words',
function ( { suite, viewport, expected } ) {
process.env.TARGET_DEVICE = viewport;
expect( createSuiteTitle( suite ) ).toStrictEqual( expected );
}
);
} );
} );
|