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

Commit e77f910

Browse files
author
Kent C. Dodds
committed
Merge pull request #626 from cynicaldevil/master
fix(formly-form): added originalModel and formOptions to getFormlyFie…
2 parents 817262d + 3e66972 commit e77f910

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
lines changed

src/directives/formly-form.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
369369
options: field,
370370
index,
371371
formState: $scope.options.formState,
372+
originalModel: $scope.model,
373+
formOptions: $scope.options,
372374
formId: $scope.formId,
373375
}
374376
}

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.parentScope.fieldModel.foo //since the scope passed to the function belongs to the form,
574+
}, //we store the outer(parent) scope in 'data' property to access
575+
data: { //the template named 'foo' stored in the fields array
576+
parentScope: scope, //the parent scope(one used to compile the form)
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)