Skip to content

Commit 3050ec3

Browse files
tests: update
1 parent cbd7a55 commit 3050ec3

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

__tests__/storage/cookie.test.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Cookie, Priority, SameSite } from '@/storage/Cookie'
1+
import { Cookie, Priority, RawCookie, SameSite } from '@/storage/Cookie'
22

33

44
describe( 'Cookie', () => {
@@ -145,7 +145,7 @@ describe( 'Cookie', () => {
145145

146146
describe( 'Cookie.toString()', () => {
147147

148-
const cookie = Cookie.parse( {
148+
const options: RawCookie = {
149149
name : 'cookiename',
150150
value : { test: 'value' },
151151
path : '/specific-path',
@@ -157,11 +157,39 @@ describe( 'Cookie', () => {
157157
sameSite : SameSite.Lax,
158158
maxAge : Date.now() + 30 * 60 * 1000,
159159
partitioned : true,
160+
}
161+
const cookie = Cookie.parse( options )
162+
163+
it( 'correctly stringify a Cookie from options object', () => {
164+
const cookieString = Cookie.toString( options )
165+
166+
expect( cookieString.includes( `${ cookie.get( 'name' ) }=${ JSON.stringify( cookie.get( 'value' ) ) }` ) )
167+
.toBe( true )
168+
expect( cookieString.includes( `Expires=${ new Date( cookie.get( 'expires' )! ).toUTCString() }` ) )
169+
.toBe( true )
170+
expect( cookieString.includes( `Max-Age=${ cookie.get( 'maxAge' ) }` ) )
171+
.toBe( true )
172+
expect( cookieString.includes( `Path=${ cookie.get( 'path' ) }` ) )
173+
.toBe( true )
174+
expect( cookieString.includes( `Priority=${ cookie.get( 'priority' ) }` ) )
175+
.toBe( true )
176+
expect( cookieString.includes( `Domain=${ cookie.get( 'domain' ) }` ) )
177+
.toBe( true )
178+
expect( cookieString.includes( `HttpOnly=${ cookie.get( 'httpOnly' ) }` ) )
179+
.toBe( true )
180+
expect( cookieString.includes( `Secure=${ cookie.get( 'secure' ) }` ) )
181+
.toBe( true )
182+
expect( cookieString.includes( `SameSite=${ cookie.get( 'sameSite' ) }` ) )
183+
.toBe( true )
184+
expect( cookieString.includes( `Partitioned=${ cookie.get( 'partitioned' ) }` ) )
185+
.toBe( true )
160186
} )
161187

162-
const cookieString = Cookie.toString( cookie )
163188

164189
it( 'correctly stringify a Cookie Map', () => {
190+
191+
const cookieString = Cookie.toString( cookie )
192+
165193
expect( cookieString.includes( `${ cookie.get( 'name' ) }=${ JSON.stringify( cookie.get( 'value' ) ) }` ) )
166194
.toBe( true )
167195
expect( cookieString.includes( `Expires=${ new Date( cookie.get( 'expires' )! ).toUTCString() }` ) )
@@ -184,6 +212,17 @@ describe( 'Cookie', () => {
184212
.toBe( true )
185213
} )
186214

215+
216+
it( 'skips values with a falsey `key`', () => {
217+
console.log( 'skips values with a falsey', Cookie.toString( {
218+
name: 'testcookie',
219+
value: 'value',
220+
// @ts-expect-error fdfs
221+
[null]: '2024-10-24',
222+
} ) )
223+
224+
} )
225+
187226
} )
188227

189228

__tests__/types.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ describe( 'isTouchDevice', () => {
214214
} )
215215

216216
it( 'returns `true` if the device is a touch device', () => {
217+
Object.defineProperty( navigator, 'msMaxTouchPoints', { value: 2 } )
218+
expect( isTouchDevice() ).toBe( true )
217219
Object.defineProperty( window, 'ontouchstart', { value: true } )
218220
expect( isTouchDevice() ).toBe( true )
219221
} )

src/storage/Cookie.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export enum SameSite
5050
* Interface representing Cookie properties before it get parsed.
5151
*
5252
*/
53-
export interface RawCookie<K = string, V = string>
53+
export interface RawCookie<K = string, V = unknown>
5454
{
5555
/**
5656
* The Cookie name.
@@ -124,7 +124,7 @@ export interface RawCookie<K = string, V = string>
124124
* Interface representing Cookie properties after it get parsed.
125125
*
126126
*/
127-
export interface ParsedCookie<K = string, V = string> extends Omit<RawCookie<K, V>, 'expires'>
127+
export interface ParsedCookie<K = string, V = unknown> extends Omit<RawCookie<K, V>, 'expires'>
128128
{
129129
/**
130130
* Indicates the maximum lifetime of the cookie.
@@ -139,7 +139,7 @@ export interface ParsedCookie<K = string, V = string> extends Omit<RawCookie<K,
139139
* Map representation of a parsed Cookie.
140140
*
141141
*/
142-
export type ParsedCookieMap<K = string, V = string> = TypedMap<ParsedCookie<K, V>, false>
142+
export type ParsedCookieMap<K = string, V = unknown> = TypedMap<ParsedCookie<K, V>, false>
143143

144144

145145
/**
@@ -169,7 +169,7 @@ export class Cookie
169169
* @param options The cookie options or a parsed Cookie Map.
170170
* @returns The set Cookie Map if successful, `false` otherwise.
171171
*/
172-
static set<K = string, V = string>( options: RawCookie<K, V> | ParsedCookieMap<K, V> )
172+
static set<K = string, V = unknown>( options: RawCookie<K, V> | ParsedCookieMap<K, V> )
173173
{
174174
const cookie = options instanceof Map ? options : Cookie.parse( options )
175175

@@ -205,7 +205,7 @@ export class Cookie
205205
* @param options The Cookie options.
206206
* @returns The parsed Cookie Map.
207207
*/
208-
static parse<K = string, V = string>( options: RawCookie<K, V> ): ParsedCookieMap<K, V>
208+
static parse<K = string, V = unknown>( options: RawCookie<K, V> ): ParsedCookieMap<K, V>
209209
{
210210
const expires = options.expires ? new Date( options.expires ) : undefined
211211
const cookie = getTypedMap<ParsedCookie<K, V>, false>()
@@ -234,7 +234,7 @@ export class Cookie
234234
* @param options The cookie options or a parsed Cookie Map.
235235
* @returns The stringified Cookie ready to be stored.
236236
*/
237-
static toString<K = string, V = string>( options: RawCookie<K, V> | ParsedCookieMap<K, V> )
237+
static toString<K = string, V = unknown>( options: RawCookie<K, V> | ParsedCookieMap<K, V> )
238238
{
239239
const cookie = options instanceof Map ? options : Cookie.parse( options )
240240

@@ -249,8 +249,7 @@ export class Cookie
249249
return (
250250
[ nameValue, ...values ]
251251
.map( ( [ key, value ] ) => {
252-
if ( ! key ) return null
253-
key = key !== name ? ucFirst( key.toString() ) as K : key
252+
key = key !== name ? ucFirst( key!.toString() ) as K : key
254253

255254
if ( key === 'Expires' && isValidDate( value ) ) {
256255
value = value.toUTCString()
@@ -273,7 +272,7 @@ export class Cookie
273272
* @param cookie The cookie string.
274273
* @returns The parsed Cookie Map or `null` if parsing fails.
275274
*/
276-
static fromString<K = string, V = string>( cookie: string ): ParsedCookieMap<K, V> | null
275+
static fromString<K = string, V = unknown>( cookie: string ): ParsedCookieMap<K, V> | null
277276
{
278277
const [ kv, ...rawValues ] = cookie.split( ';' )
279278

0 commit comments

Comments
 (0)