Skip to content

Commit

Permalink
made events immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jonathan committed Jun 26, 2024
1 parent 4888bdd commit cac47b1
Show file tree
Hide file tree
Showing 5 changed files with 879 additions and 339 deletions.
79 changes: 8 additions & 71 deletions __tests__/Event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,57 +87,23 @@ describe('Event', () => {
const e1 = makeUser({
id,
createdAt,
name: 'jonathan',
name,
})

e1.name = 'daniel'

expect(e1.id).toBe(id)
expect(e1.createdAt).toBe(createdAt)
expect(e1.name).toBe(name)
})

it('partial validator', () => {
it('immutable properties', () => {
const id = '123'
const createdAt = new Date()
const name = 'daniel'

const e1 = makeUser({
id,
createdAt,
name: 'jonathan',
})

try {
e1.name = ''
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof EventError) {
expect(error.name).toBe('EventError')
expectTypeOf(error.message).toMatchTypeOf<string>()
}
else {
expect(true).toBeFalsy()
}
}

expect(e1.id).toBe(id)
expect(e1.createdAt).toBe(createdAt)
expect(name).not.toBe(e1.name)
})

it('partial validator 2', () => {
const id = '123'
const createdAt = new Date()
const name = 'daniel'
const empty = 'yay'

const e1 = makeUser({
id,
createdAt,
name: 'jonathan',
empty,
name,
})

try {
Expand All @@ -156,11 +122,10 @@ describe('Event', () => {

expect(e1.id).toBe(id)
expect(e1.createdAt).toBe(createdAt)
expect(name).not.toBe(e1.name)
expect(empty).toBe(e1.empty)
expect(name).toBe(e1.name)
})

it('partial validator 3', () => {
it('undefined properties', () => {
const id = '123'
const createdAt = new Date()
const name = 'daniel'
Expand All @@ -169,27 +134,13 @@ describe('Event', () => {
const e1 = makeUser({
id,
createdAt,
name: 'jonathan',
name,
empty,
})

try {
e1.name = ''
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof EventError) {
expect(error.name).toBe('EventError')
expectTypeOf(error.message).toMatchTypeOf<string>()
}
else {
expect(true).toBeFalsy()
}
}

expect(e1.id).toBe(id)
expect(e1.createdAt).toBe(createdAt)
expect(name).not.toBe(e1.name)
expect(name).toBe(e1.name)
expect(empty).toBe(undefined)
})

Expand All @@ -199,10 +150,6 @@ describe('Event', () => {
const name = 'daniel'

const createEvent = defineEvent<User>({
trace(event) {
expect(guard(event)).toBeTruthy()
},

created(event) {
expect(guard(event))
},
Expand Down Expand Up @@ -233,13 +180,6 @@ describe('Event', () => {
expect(2 < event.name.length).toBeTruthy()
return 2 < value.length
},
updated: (newValue, oldValue, event) => {
expect(newValue).toBe('jonathan')
expect(oldValue).toBe(name)
expect(event.id).toBe(id)
expect(event.createdAt).toBe(createdAt)
expect(event.name).toBe(name)
},
},
},
})
Expand All @@ -252,9 +192,6 @@ describe('Event', () => {

expect(e1.id).toBe(id)
expect(e1.createdAt).toBe(createdAt)

e1.name = 'jonathan'

expect('jonathan').toBe(e1.name)
expect(e1.name).toBe(e1.name)
})
})
Loading

0 comments on commit cac47b1

Please sign in to comment.