Files
archived-next-auth/packages/adapter-dynamodb/tests/format.test.ts
Nico Domino dc3ad8c408 chore: move adapters to monorepo (#3805)
* feat: move adapters repo to new packages dir

* fix: rm docusaurus build dir

* fix: update .gitignore

* fix: reorganise package directories

* remove package lock files

* fix: folder rename

* remove package lock file

* fix: jest config paths

* update yarn.lock

* ignore dynamodb local bin

* fix: gitignore

* fix: update adapter-test

* change adapter-test package json

* rename prisma adapter package name

* fix paths

* update gitignore

* run tests with one concurrency

* fix: merge conflicts

* gitignore dist folders

* fix: add jest.config.js to tsconfig ignore

* fix: yarn.lock

* fix: ignore pouch in turbo commands

* ignore jest file

* fix: test turbo test cmd

* fix: turbo test cmd

* test: disable mongodb-adapter temporarily

* ignore all dev.db files

* simplify gitignore

* remove unused dependency

* have tsconfig in its own package

* remove unnecessary .gitignore files

* move jest config to preset

* add ts expect error comment

* chore: update .gitignore

* remove babelrc

* don't depend on build for testing in turbo

* fix: cleanup testing npm scripts

* fix: remove jest-config roots

* fix: add fauna jest preset

* fix: rm dev.db from prisma mirgation

* fix prisma

* remove nohoist

Co-authored-by: Balázs Orbán <info@balazsorban.com>
2022-02-04 22:40:32 +01:00

122 lines
3.0 KiB
TypeScript

import { format } from "../src/utils"
describe("dynamodb utils.format", () => {
it("format.to() preserves non-Date non-expires properties", () => {
expect(
format.to({
pk: "test-pk",
email: "test@example.com",
})
).toEqual({
pk: "test-pk",
email: "test@example.com",
})
})
it("format.to() converts non-expires Date properties to ISO strings", () => {
const date = new Date()
expect(
format.to({
dateProp: date,
})
).toEqual({
dateProp: date.toISOString(),
})
})
it("format.to() converts expires property to a UNIX timestamp", () => {
// DynamoDB requires that the property used for TTL is a UNIX timestamp.
const date = new Date()
const timestamp = date.getTime() / 1000
expect(
format.to({
expires: date,
})
).toEqual({
expires: timestamp,
})
})
it("format.from() preserves non-special attributes", () => {
expect(
format.from({
testAttr1: "test-value",
testAttr2: 5,
})
).toEqual({
testAttr1: "test-value",
testAttr2: 5,
})
})
it("format.from() removes dynamodb key attributes", () => {
expect(
format.from({
pk: "test-pk",
sk: "test-sk",
GSI1PK: "test-GSI1PK",
GSI1SK: "test-GSI1SK",
})
).toEqual({})
})
it("format.from() only removes type attribute from Session, VT, and User", () => {
expect(format.from({ type: "SESSION" })).toEqual({})
expect(format.from({ type: "VT" })).toEqual({})
expect(format.from({ type: "USER" })).toEqual({})
expect(format.from({ type: "ANYTHING" })).toEqual({ type: "ANYTHING" })
expect(format.from({ type: "ELSE" })).toEqual({ type: "ELSE" })
})
it("format.from() converts ISO strings to Date instances", () => {
const date = new Date()
expect(
format.from({
someDate: date.toISOString(),
})
).toEqual({
someDate: date,
})
})
it("format.from() converts expires attribute from timestamp to Date instance", () => {
// AdapterSession["expires"] and VerificationToken["expires"] are both meant
// to be Date instances.
const date = new Date()
const timestamp = date.getTime() / 1000
expect(
format.from({
expires: timestamp,
})
).toEqual({
expires: date,
})
})
it("format.from() converts expires attribute from ISO string to Date instance", () => {
// Due to a bug in an old version, some expires attributes were stored as
// ISO strings, so we need to handle those properly too.
const date = new Date()
expect(
format.from({
expires: date.toISOString(),
})
).toEqual({
expires: date,
})
})
it("format.from(format.to()) preserves expires attribute", () => {
const date = new Date()
expect(
format.from(
format.to({
expires: date,
})
)
).toEqual({
expires: date,
})
})
})