Skip to content

Commit 051f1a0

Browse files
committed
Refactor Webauthn unit tests to use specific types for input and expected body, improving type safety and clarity.
1 parent 0176e46 commit 051f1a0

File tree

1 file changed

+95
-50
lines changed

1 file changed

+95
-50
lines changed

packages/commerce-sdk-react/src/auth/index.test.ts

Lines changed: 95 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,17 +1343,26 @@ describe('Webauthn', () => {
13431343
channel_id: config.siteId
13441344
}
13451345
]
1346-
])('authorizeWebauthnRegistration %s', async (_, input: any, expectedBody: any) => {
1347-
const auth = new Auth(config)
1348-
await auth.authorizeWebauthnRegistration(input)
1346+
])(
1347+
'authorizeWebauthnRegistration %s',
1348+
async (
1349+
_,
1350+
input: Partial<ShopperLoginTypes.authorizeWebauthnRegistrationBodyType>,
1351+
expectedBody: Partial<ShopperLoginTypes.authorizeWebauthnRegistrationBodyType>
1352+
) => {
1353+
const auth = new Auth(config)
1354+
await auth.authorizeWebauthnRegistration(
1355+
input as ShopperLoginTypes.authorizeWebauthnRegistrationBodyType
1356+
)
13491357

1350-
expect((auth as any).client.authorizeWebauthnRegistration).toHaveBeenCalledWith({
1351-
headers: {
1352-
Authorization: ''
1353-
},
1354-
body: expectedBody
1355-
})
1356-
})
1358+
expect((auth as any).client.authorizeWebauthnRegistration).toHaveBeenCalledWith({
1359+
headers: {
1360+
Authorization: ''
1361+
},
1362+
body: expectedBody
1363+
})
1364+
}
1365+
)
13571366

13581367
test.each([
13591368
[
@@ -1387,17 +1396,26 @@ describe('Webauthn', () => {
13871396
channel_id: config.siteId
13881397
}
13891398
]
1390-
])('startWebauthnUserRegistration %s', async (_, input: any, expectedBody: any) => {
1391-
const auth = new Auth(config)
1392-
await auth.startWebauthnUserRegistration(input)
1399+
])(
1400+
'startWebauthnUserRegistration %s',
1401+
async (
1402+
_,
1403+
input: Partial<ShopperLoginTypes.startWebauthnUserRegistrationBodyType>,
1404+
expectedBody: Partial<ShopperLoginTypes.startWebauthnUserRegistrationBodyType>
1405+
) => {
1406+
const auth = new Auth(config)
1407+
await auth.startWebauthnUserRegistration(
1408+
input as ShopperLoginTypes.startWebauthnUserRegistrationBodyType
1409+
)
13931410

1394-
expect((auth as any).client.startWebauthnUserRegistration).toHaveBeenCalledWith({
1395-
headers: {
1396-
Authorization: ''
1397-
},
1398-
body: expectedBody
1399-
})
1400-
})
1411+
expect((auth as any).client.startWebauthnUserRegistration).toHaveBeenCalledWith({
1412+
headers: {
1413+
Authorization: ''
1414+
},
1415+
body: expectedBody
1416+
})
1417+
}
1418+
)
14011419

14021420
test.each([
14031421
[
@@ -1432,17 +1450,26 @@ describe('Webauthn', () => {
14321450
client_id: config.clientId
14331451
}
14341452
]
1435-
])('finishWebauthnUserRegistration %s', async (_, input: any, expectedBody: any) => {
1436-
const auth = new Auth(config)
1437-
await auth.finishWebauthnUserRegistration(input)
1453+
])(
1454+
'finishWebauthnUserRegistration %s',
1455+
async (
1456+
_,
1457+
input: Partial<ShopperLoginTypes.RegistrationFinishRequest>,
1458+
expectedBody: Partial<ShopperLoginTypes.RegistrationFinishRequest>
1459+
) => {
1460+
const auth = new Auth(config)
1461+
await auth.finishWebauthnUserRegistration(
1462+
input as ShopperLoginTypes.RegistrationFinishRequest
1463+
)
14381464

1439-
expect((auth as any).client.finishWebauthnUserRegistration).toHaveBeenCalledWith({
1440-
headers: {
1441-
Authorization: ''
1442-
},
1443-
body: expectedBody
1444-
})
1445-
})
1465+
expect((auth as any).client.finishWebauthnUserRegistration).toHaveBeenCalledWith({
1466+
headers: {
1467+
Authorization: ''
1468+
},
1469+
body: expectedBody
1470+
})
1471+
}
1472+
)
14461473

14471474
test.each([
14481475
[
@@ -1468,17 +1495,26 @@ describe('Webauthn', () => {
14681495
client_id: config.clientId
14691496
}
14701497
]
1471-
])('startWebauthnAuthentication %s', async (_, input: any, expectedBody: any) => {
1472-
const auth = new Auth(config)
1473-
await auth.startWebauthnAuthentication(input)
1498+
])(
1499+
'startWebauthnAuthentication %s',
1500+
async (
1501+
_,
1502+
input: Partial<ShopperLoginTypes.startWebauthnAuthenticationBodyType>,
1503+
expectedBody: Partial<ShopperLoginTypes.startWebauthnAuthenticationBodyType>
1504+
) => {
1505+
const auth = new Auth(config)
1506+
await auth.startWebauthnAuthentication(
1507+
input as ShopperLoginTypes.startWebauthnAuthenticationBodyType
1508+
)
14741509

1475-
expect((auth as any).client.startWebauthnAuthentication).toHaveBeenCalledWith({
1476-
headers: {
1477-
Authorization: ''
1478-
},
1479-
body: expectedBody
1480-
})
1481-
})
1510+
expect((auth as any).client.startWebauthnAuthentication).toHaveBeenCalledWith({
1511+
headers: {
1512+
Authorization: ''
1513+
},
1514+
body: expectedBody
1515+
})
1516+
}
1517+
)
14821518

14831519
test.each([
14841520
[
@@ -1513,15 +1549,24 @@ describe('Webauthn', () => {
15131549
credential: PUBLIC_KEY_CREDENTIAL_JSON
15141550
}
15151551
]
1516-
])('finishWebauthnAuthentication %s', async (_, input: any, expectedBody: any) => {
1517-
const auth = new Auth(config)
1518-
await auth.finishWebauthnAuthentication(input)
1552+
])(
1553+
'finishWebauthnAuthentication %s',
1554+
async (
1555+
_,
1556+
input: Partial<ShopperLoginTypes.AuthenticateFinishRequest>,
1557+
expectedBody: Partial<ShopperLoginTypes.AuthenticateFinishRequest>
1558+
) => {
1559+
const auth = new Auth(config)
1560+
await auth.finishWebauthnAuthentication(
1561+
input as ShopperLoginTypes.AuthenticateFinishRequest
1562+
)
15191563

1520-
expect((auth as any).client.finishWebauthnAuthentication).toHaveBeenCalledWith({
1521-
headers: {
1522-
Authorization: ''
1523-
},
1524-
body: expectedBody
1525-
})
1526-
})
1564+
expect((auth as any).client.finishWebauthnAuthentication).toHaveBeenCalledWith({
1565+
headers: {
1566+
Authorization: ''
1567+
},
1568+
body: expectedBody
1569+
})
1570+
}
1571+
)
15271572
})

0 commit comments

Comments
 (0)