From 71a79f1abe2e8df93436f7433ce190422c3bb3f5 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Fri, 10 Apr 2020 16:02:10 -0400 Subject: [PATCH 1/3] Add toJSON method to Remote Config Template implementation --- src/remote-config/remote-config.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/remote-config/remote-config.ts b/src/remote-config/remote-config.ts index 9fcf6bcbf6..f7c78d4543 100644 --- a/src/remote-config/remote-config.ts +++ b/src/remote-config/remote-config.ts @@ -141,7 +141,7 @@ class RemoteConfigTemplateImpl implements RemoteConfigTemplate { !validator.isNonEmptyString(config.etag)) { throw new FirebaseRemoteConfigError( 'invalid-argument', - `Invalid Remote Config template response: ${JSON.stringify(config)}`); + `Invalid Remote Config template: ${JSON.stringify(config)}`); } this.etagInternal = config.etag; @@ -188,4 +188,16 @@ class RemoteConfigTemplateImpl implements RemoteConfigTemplate { get etag(): string { return this.etagInternal; } + + /** + * @return {RemoteConfigTemplate} A JSON-serializable representation of this object. + */ + public toJSON(): RemoteConfigTemplate { + return { + etag: this.etagInternal, + parameters: this.parameters, + parameterGroups: this.parameterGroups, + conditions: this.conditions, + } + } } From afec9b52521001142b9e85efd7bb91dd51764a5c Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Fri, 10 Apr 2020 16:14:56 -0400 Subject: [PATCH 2/3] Add unit tests for JSON.stringify --- src/remote-config/remote-config.ts | 4 ++-- test/integration/remote-config.spec.ts | 2 +- test/unit/remote-config/remote-config.spec.ts | 16 +++++++++------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/remote-config/remote-config.ts b/src/remote-config/remote-config.ts index f7c78d4543..48b3c77dc4 100644 --- a/src/remote-config/remote-config.ts +++ b/src/remote-config/remote-config.ts @@ -194,10 +194,10 @@ class RemoteConfigTemplateImpl implements RemoteConfigTemplate { */ public toJSON(): RemoteConfigTemplate { return { - etag: this.etagInternal, + conditions: this.conditions, parameters: this.parameters, parameterGroups: this.parameterGroups, - conditions: this.conditions, + etag: this.etagInternal, } } } diff --git a/test/integration/remote-config.spec.ts b/test/integration/remote-config.spec.ts index b097016de7..2e57ebfc01 100644 --- a/test/integration/remote-config.spec.ts +++ b/test/integration/remote-config.spec.ts @@ -179,7 +179,7 @@ describe('admin.remoteConfig', () => { const jsonString = JSON.stringify(invalidEtagTemplate); it(`should throw if the ETag is ${JSON.stringify(invalidEtag)}`, () => { expect(() => admin.remoteConfig().createTemplateFromJSON(jsonString)) - .to.throw(`Invalid Remote Config template response: ${jsonString}`); + .to.throw(`Invalid Remote Config template: ${jsonString}`); }); }); diff --git a/test/unit/remote-config/remote-config.spec.ts b/test/unit/remote-config/remote-config.spec.ts index 4e5b56d34d..a89edd5dd2 100644 --- a/test/unit/remote-config/remote-config.spec.ts +++ b/test/unit/remote-config/remote-config.spec.ts @@ -186,7 +186,7 @@ describe('RemoteConfig', () => { stubs.push(stub); return remoteConfig.getTemplate() .should.eventually.be.rejected.and.have.property( - 'message', 'Invalid Remote Config template response: null'); + 'message', 'Invalid Remote Config template: null'); }); it('should reject when API response does not contain an ETag', () => { @@ -198,7 +198,7 @@ describe('RemoteConfig', () => { stubs.push(stub); return remoteConfig.getTemplate() .should.eventually.be.rejected.and.have.property( - 'message', `Invalid Remote Config template response: ${JSON.stringify(response)}`); + 'message', `Invalid Remote Config template: ${JSON.stringify(response)}`); }); it('should reject when API response does not contain valid parameters', () => { @@ -314,6 +314,8 @@ describe('RemoteConfig', () => { expect(cond.name).to.equal('ios'); expect(cond.expression).to.equal('device.os == \'ios\''); expect(cond.tagColor).to.equal(TagColor.BLUE); + + expect(JSON.stringify(template)).equals(JSON.stringify(REMOTE_CONFIG_RESPONSE)); }); }); }); @@ -344,7 +346,7 @@ describe('RemoteConfig', () => { stubs.push(stub); return remoteConfig.validateTemplate(REMOTE_CONFIG_TEMPLATE) .should.eventually.be.rejected.and.have.property( - 'message', 'Invalid Remote Config template response: null'); + 'message', 'Invalid Remote Config template: null'); }); it('should reject when API response does not contain an ETag', () => { @@ -356,7 +358,7 @@ describe('RemoteConfig', () => { stubs.push(stub); return remoteConfig.validateTemplate(REMOTE_CONFIG_TEMPLATE) .should.eventually.be.rejected.and.have.property( - 'message', `Invalid Remote Config template response: ${JSON.stringify(response)}`); + 'message', `Invalid Remote Config template: ${JSON.stringify(response)}`); }); it('should reject when API response does not contain valid parameters', () => { @@ -497,7 +499,7 @@ describe('RemoteConfig', () => { stubs.push(stub); return remoteConfig.publishTemplate(REMOTE_CONFIG_TEMPLATE) .should.eventually.be.rejected.and.have.property( - 'message', 'Invalid Remote Config template response: null'); + 'message', 'Invalid Remote Config template: null'); }); it('should reject when API response does not contain an ETag', () => { @@ -509,7 +511,7 @@ describe('RemoteConfig', () => { stubs.push(stub); return remoteConfig.publishTemplate(REMOTE_CONFIG_TEMPLATE) .should.eventually.be.rejected.and.have.property( - 'message', `Invalid Remote Config template response: ${JSON.stringify(response)}`); + 'message', `Invalid Remote Config template: ${JSON.stringify(response)}`); }); it('should reject when API response does not contain valid parameters', () => { @@ -657,7 +659,7 @@ describe('RemoteConfig', () => { const jsonString = JSON.stringify(sourceTemplate); it(`should throw if the ETag is ${JSON.stringify(invalidEtag)}`, () => { expect(() => remoteConfig.createTemplateFromJSON(jsonString)) - .to.throw(`Invalid Remote Config template response: ${jsonString}`); + .to.throw(`Invalid Remote Config template: ${jsonString}`); }); }); From be401379b731a5d1b7b911d9c4f386724843f93a Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Fri, 10 Apr 2020 16:58:14 -0400 Subject: [PATCH 3/3] PR fixes --- src/remote-config/remote-config.ts | 2 +- test/unit/remote-config/remote-config.spec.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/remote-config/remote-config.ts b/src/remote-config/remote-config.ts index 48b3c77dc4..7045ba00bb 100644 --- a/src/remote-config/remote-config.ts +++ b/src/remote-config/remote-config.ts @@ -197,7 +197,7 @@ class RemoteConfigTemplateImpl implements RemoteConfigTemplate { conditions: this.conditions, parameters: this.parameters, parameterGroups: this.parameterGroups, - etag: this.etagInternal, + etag: this.etag, } } } diff --git a/test/unit/remote-config/remote-config.spec.ts b/test/unit/remote-config/remote-config.spec.ts index a89edd5dd2..235bea2c76 100644 --- a/test/unit/remote-config/remote-config.spec.ts +++ b/test/unit/remote-config/remote-config.spec.ts @@ -315,7 +315,8 @@ describe('RemoteConfig', () => { expect(cond.expression).to.equal('device.os == \'ios\''); expect(cond.tagColor).to.equal(TagColor.BLUE); - expect(JSON.stringify(template)).equals(JSON.stringify(REMOTE_CONFIG_RESPONSE)); + const parsed = JSON.parse(JSON.stringify(template)); + expect(parsed).deep.equals(REMOTE_CONFIG_RESPONSE); }); }); });