11import { describe , it , expect , beforeEach , afterEach , vi } from "bun:test" ;
2- import { createContactObject , createContact , notion } from "./notion" ;
2+ import { createContact , notion } from "./notion" ;
33
44const mock = vi . spyOn ( notion . pages , "create" ) . mockResolvedValue ( {
55 object : "page" ,
@@ -31,38 +31,6 @@ describe("Notion Helper", () => {
3131 databaseID : "mock-database-id" ,
3232 source : "website" ,
3333 } ;
34- describe ( "createContactObject" , ( ) => {
35- it ( "should create correct Notion payload structure" , ( ) => {
36- const payload = createContactObject (
37- validContactData . id ,
38- validContactData . email ,
39- validContactData . name ,
40- validContactData . content ,
41- validContactData . databaseID ,
42- validContactData . source ,
43- ) ;
44-
45- expect ( payload ) . toHaveProperty ( "parent" ) ;
46- expect ( payload ) . toHaveProperty ( "properties" ) ;
47- expect ( payload ) . toHaveProperty ( "children" ) ;
48-
49- expect ( payload . parent . database_id ) . toBe ( "mock-database-id" ) ;
50-
51- expect ( payload . properties . id . title [ 0 ] . text . content ) . toBe ( "test-id-123" ) ;
52- expect ( payload . properties . email . email ) . toBe ( "john@example.com" ) ;
53- expect ( payload . properties . name . rich_text [ 0 ] . text . content ) . toBe (
54- "John Doe" ,
55- ) ;
56- expect ( payload . properties . source . rich_text [ 0 ] . text . content ) . toBe (
57- "website" ,
58- ) ;
59-
60- expect ( payload . children [ 0 ] . paragraph . rich_text [ 0 ] . text ?. content ) . toBe (
61- "Test message" ,
62- ) ;
63- } ) ;
64- } ) ;
65-
6634 describe ( "createContact" , ( ) => {
6735 it ( "should validate required fields" , ( ) => {
6836 const requiredFields = [ "id" , "email" , "name" , "databaseID" ] ;
@@ -72,7 +40,7 @@ describe("Notion Helper", () => {
7240 } ) ;
7341 } ) ;
7442
75- it ( "should create Notion page and return id and url" , async ( ) => {
43+ it ( "should create Notion page and return valid url" , async ( ) => {
7644 const response = await createContact (
7745 validContactData . id ,
7846 validContactData . email ,
@@ -82,19 +50,83 @@ describe("Notion Helper", () => {
8250 validContactData . source ,
8351 ) ;
8452
85- expect ( response . id ) . toBe ( "fake-page-id" ) ;
86- expect ( response . url ) . toContain ( "www.notion.so" ) ;
87- expect ( response . error ) . toBe ( undefined ) ;
8853 expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
54+ expect ( mock ) . toHaveBeenCalledWith (
55+ expect . objectContaining ( {
56+ parent : {
57+ database_id : "mock-database-id" ,
58+ } ,
59+ } ) ,
60+ ) ;
61+ expect ( mock ) . toHaveBeenCalledWith (
62+ expect . objectContaining ( {
63+ properties : expect . objectContaining ( {
64+ email : {
65+ email : "john@example.com" ,
66+ } ,
67+ } ) ,
68+ } ) ,
69+ ) ;
70+ expect ( mock ) . toHaveBeenCalledWith (
71+ expect . objectContaining ( {
72+ properties : expect . objectContaining ( {
73+ name : {
74+ rich_text : [
75+ {
76+ text : {
77+ content : "John Doe" ,
78+ } ,
79+ } ,
80+ ] ,
81+ } ,
82+ } ) ,
83+ } ) ,
84+ ) ;
85+ expect ( mock ) . toHaveBeenCalledWith (
86+ expect . objectContaining ( {
87+ properties : expect . objectContaining ( {
88+ source : {
89+ rich_text : [
90+ {
91+ text : {
92+ content : "website" ,
93+ } ,
94+ } ,
95+ ] ,
96+ } ,
97+ } ) ,
98+ } ) ,
99+ ) ;
100+ expect ( mock ) . toHaveBeenCalledWith (
101+ expect . objectContaining ( {
102+ children : expect . arrayContaining ( [
103+ expect . objectContaining ( {
104+ paragraph : {
105+ rich_text : [
106+ {
107+ text : {
108+ content : "Test message" ,
109+ } ,
110+ } ,
111+ ] ,
112+ } ,
113+ } ) ,
114+ ] ) ,
115+ } ) ,
116+ ) ;
117+ expect ( response ) . toHaveProperty (
118+ "url" ,
119+ "https://www.notion.so/fakepageid" ,
120+ ) ;
121+ expect ( response ) . not . toHaveProperty ( "error" ) ;
89122 } ) ;
90123
91124 it ( "should return error message if data is missing" , async ( ) => {
92125 const response = await createContact ( "" , "" , "" , "" , "" , "" ) ;
93126
94- expect ( response . id ) . toBe ( undefined ) ;
95- expect ( response . url ) . toBe ( undefined ) ;
96- expect ( response . error ?. length ) . toBeGreaterThan ( 0 ) ;
97127 expect ( mock ) . toHaveBeenCalledTimes ( 0 ) ;
128+ expect ( response ) . not . toHaveProperty ( "url" ) ;
129+ expect ( response ) . toHaveProperty ( "error" ) ;
98130 } ) ;
99131
100132 it ( "should return error message if page was not created" , async ( ) => {
@@ -112,10 +144,9 @@ describe("Notion Helper", () => {
112144 validContactData . source ,
113145 ) ;
114146
115- expect ( response . id ) . toBe ( undefined ) ;
116- expect ( response . url ) . toBe ( undefined ) ;
117- expect ( response . error ?. length ) . toBeGreaterThan ( 0 ) ;
118147 expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
148+ expect ( response ) . not . toHaveProperty ( "url" ) ;
149+ expect ( response ) . toHaveProperty ( "error" ) ;
119150 } ) ;
120151 } ) ;
121152} ) ;
0 commit comments