Skip to content

Commit

Permalink
fix(Condition): do not throw if no condition matched
Browse files Browse the repository at this point in the history
This allow to create property that won't be read but will
still execute the PrePost operations.
  • Loading branch information
tperale committed Dec 8, 2024
1 parent 3fa56e0 commit 3d06c60
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
17 changes: 0 additions & 17 deletions src/decorators/__tests__/condition.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, expect } from '@jest/globals'
import { useConditions, IfThen, Else, Choice } from '../condition'
import { type RelationTypeProperty, type PrimitiveTypeProperty } from '../primitive'
import { NoConditionMatched } from '../../error'
import Meta from '../../metadatas'

function testCondition<This> (TargetClass: new (...args: any) => This, relation: any, post?: (relation: PrimitiveTypeProperty<This> | RelationTypeProperty<This, any> | undefined, instance: This) => void, field: keyof This = 'field' as keyof This) {
Expand Down Expand Up @@ -119,19 +118,3 @@ describe('@Condition: basic testing', () => {
})
})
})

describe('@Condition: error testing', () => {
it('NoConditionMatched: should alway define a valid option', () => {
class TestClass {
@IfThen(_ => false, Number)
field: number
}

const instance = new TestClass()
const conditions = Meta.getConditions(TestClass[Symbol.metadata] as DecoratorMetadataObject, 'field')

expect(() => {
useConditions(conditions, instance)
}).toThrow(NoConditionMatched)
})
})
5 changes: 4 additions & 1 deletion src/decorators/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,10 @@ export function useConditions<This, Value> (conditions: Array<Condition<This>>,
// - cursor backtrace to show the position in the file
// - current instance to print. The condition are mostly done on the current instance
// - If creating a `@Choice` it's probably more easy to debug.
throw new NoConditionMatched()
// It's still unclear if I need to throw an error or not. Right now usecases
// seems to indicate it's better to not throw.
// throw new NoConditionMatched()
return undefined
}

return cond.relation
Expand Down

0 comments on commit 3d06c60

Please sign in to comment.