-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added test for save-tosend-message
- Loading branch information
1 parent
76cdb3a
commit f5b3ed6
Showing
3 changed files
with
125 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { messageSchema } from "../schemas/tosend-messages.schema"; | ||
|
||
interface MessageParams { | ||
userId: string; | ||
text: string; | ||
timestamp: number; | ||
meta?: Record<string, unknown>; | ||
} | ||
|
||
// Test factories | ||
function createValidMessage(overrides?: Partial<MessageParams>): MessageParams { | ||
return { | ||
userId: "user123", | ||
text: "Hello, this is a test message", | ||
timestamp: Date.now(), | ||
...overrides, | ||
}; | ||
} | ||
|
||
describe("Save Tosend Message Schema Validation", () => { | ||
describe("valid messages", () => { | ||
it("should parse a complete valid message", () => { | ||
const validMessage = createValidMessage(); | ||
const result = messageSchema.parse(validMessage); | ||
expect(result).toEqual(validMessage); | ||
}); | ||
|
||
it("should accept messages with meta data", () => { | ||
const validMessage = createValidMessage({ | ||
meta: { | ||
priority: "high", | ||
category: "notification", | ||
tags: ["important", "urgent"], | ||
}, | ||
}); | ||
const result = messageSchema.parse(validMessage); | ||
expect(result).toEqual(validMessage); | ||
}); | ||
|
||
it("should accept valid timestamps", () => { | ||
const validMessage = createValidMessage({ | ||
timestamp: 1234567890, | ||
}); | ||
const result = messageSchema.parse(validMessage); | ||
expect(result).toEqual(validMessage); | ||
}); | ||
}); | ||
|
||
describe("invalid messages", () => { | ||
it("should throw an error for missing required fields", () => { | ||
const invalidMessage = { | ||
userId: "user123", | ||
// Missing other required fields | ||
}; | ||
|
||
expect(() => messageSchema.parse(invalidMessage)).toThrow(); | ||
}); | ||
|
||
it("should throw an error for empty text", () => { | ||
const invalidMessage = createValidMessage({ | ||
text: "", | ||
}); | ||
expect(() => messageSchema.parse(invalidMessage)).toThrow(); | ||
}); | ||
|
||
it("should throw an error for invalid timestamp type", () => { | ||
const invalidMessage = createValidMessage({ | ||
// @ts-expect-error Testing invalid timestamp type | ||
timestamp: "not a number", | ||
}); | ||
expect(() => messageSchema.parse(invalidMessage)).toThrow(); | ||
}); | ||
|
||
it("should throw an error for invalid userId type", () => { | ||
const invalidMessage = createValidMessage({ | ||
// @ts-expect-error Testing invalid userId type | ||
userId: 123, | ||
}); | ||
expect(() => messageSchema.parse(invalidMessage)).toThrow(); | ||
}); | ||
|
||
it("should throw an error for missing userId", () => { | ||
const invalidMessage = createValidMessage(); | ||
const { userId, ...invalidMessageWithoutUserId } = invalidMessage; | ||
expect(() => messageSchema.parse(invalidMessageWithoutUserId)).toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters