Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit fcb91c8

Browse files
committed
test(formly-form): closes issue #616
added tests to hideExpression test suite
1 parent d87392a commit fcb91c8

File tree

1 file changed

+74
-15
lines changed

1 file changed

+74
-15
lines changed

src/directives/formly-form.test.js

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -536,26 +536,85 @@ describe('formly-form', () => {
536536
beforeEach(() => {
537537
scope.model = {}
538538
scope.fieldModel = {}
539-
540-
scope.fields = [
541-
{template: input, key: 'foo', model: scope.fieldModel},
542-
{template: input, key: 'bar', model: scope.fieldModel, hideExpression: () => !!scope.fieldModel.foo},
543-
]
544539
})
540+
describe('behaviour when model changes', () => {
541+
542+
describe('when a string is passed to hideExpression', () => {
543+
beforeEach(() => {
544+
scope.fields = [
545+
{template: input, key: 'foo', model: scope.fieldModel},
546+
{template: input, key: 'bar', model: scope.fieldModel, hideExpression: () => !!scope.fieldModel.foo},
547+
]
548+
})
545549

546-
it('should be called and resolve to true when field model changes', () => {
547-
compileAndDigest()
548-
expect(scope.fields[1].hide).be.false
549-
scope.fields[0].formControl.$setViewValue('value')
550-
expect(scope.fields[1].hide).be.true
550+
it('should be called and should resolve to true when field model changes', () => {
551+
compileAndDigest()
552+
expect(scope.fields[1].hide).be.false
553+
scope.fields[0].formControl.$setViewValue('value')
554+
expect(scope.fields[1].hide).be.true
555+
})
556+
557+
it('should be called and should resolve to false when field model changes', () => {
558+
scope.fieldModel.foo = 'value'
559+
compileAndDigest()
560+
expect(scope.fields[1].hide).be.true
561+
scope.fields[0].formControl.$setViewValue('')
562+
expect(scope.fields[1].hide).be.false
563+
})
564+
})
565+
describe('when a function is passed to hideExpression', () => {
566+
beforeEach(() => {
567+
scope.fields = [
568+
{template: input, key: 'foo', model: scope.fieldModel},
569+
{
570+
template: input, key: 'bar',
571+
model: scope.fieldModel,
572+
hideExpression: ($viewValue, $modelValue, scope) => {
573+
return !!scope.fields[1].data.formScope.fieldModel.foo //since the scope passed to the function belongs to the field,
574+
}, //we store the form's scope in data property to access it here.
575+
data: {
576+
formScope: scope, //the form's scope
577+
},
578+
},
579+
]
580+
})
581+
582+
it('should be called and should resolve to true when field model changes', () => {
583+
compileAndDigest()
584+
expect(scope.fields[1].hide).be.false
585+
scope.fields[0].formControl.$setViewValue('value')
586+
expect(scope.fields[1].hide).be.true
587+
})
588+
589+
it('should be called and should resolve to false when field model changes', () => {
590+
scope.fieldModel.foo = 'value'
591+
compileAndDigest()
592+
expect(scope.fields[1].hide).be.true
593+
scope.fields[0].formControl.$setViewValue('')
594+
expect(scope.fields[1].hide).be.false
595+
})
596+
})
551597
})
552598

553-
it('should be called and resolve to false when field model changes', () => {
554-
scope.fieldModel.foo = 'value'
599+
it('ensures that hideExpression has all the expressionProperties values', () => {
600+
scope.options = {formState: {}}
601+
scope.fields = [{
602+
template: input,
603+
key: 'test',
604+
hideExpression: `
605+
options === options.data.field &&
606+
index === 0 &&
607+
formState === options.data.formOptions.formState &&
608+
originalModel === options.data.originalModel &&
609+
formOptions === options.data.formOptions`,
610+
data: {
611+
originalModel: scope.model,
612+
formOptions: scope.options,
613+
},
614+
}]
615+
scope.fields[0].data.field = scope.fields[0]
555616
compileAndDigest()
556-
expect(scope.fields[1].hide).be.true
557-
scope.fields[0].formControl.$setViewValue('')
558-
expect(scope.fields[1].hide).be.false
617+
expect(scope.fields[0].hide).be.true
559618
})
560619
})
561620

0 commit comments

Comments
 (0)