Skip to content

Commit

Permalink
fix: Default were not returned when a store contained a key with unde…
Browse files Browse the repository at this point in the history
…fined value
  • Loading branch information
RonaldJerez committed Nov 3, 2023
1 parent 157d47e commit 54d4d1f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class Get extends CustomType {
// gets an item from storage
// temp storage (current session) first, then the persisted storage
this.item = this.item || cache._storage[this.key] || cache.storage[this.key]
let value = this._getValue(this.item || this.default, json)
let value = this._getValue(this.item, json) || this._getValue(this.default, json)

if (this.pull) {
delete cache._storage[this.key]
Expand Down
11 changes: 11 additions & 0 deletions tests/api/get.get.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@
query:
zip: !get zip
:status: 201

# Testing getting a default value when a regexp group is undefined
- :conditions:
query:
locale: !save
- languageCode
- countryCode
- !regexp '([a-z]{2})(?:\W([A-Z]{2}))?'
:response:
languageCode: !get { languageCode: 'en' }
countryCode: !get { countryCode: 'US' }
10 changes: 10 additions & 0 deletions tests/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,14 @@ describe('Data persistance', () => {

test('check conditions against saved data', () =>
request(server).get('/api/get?zip=12345').expect(201))

test('returns default when a regexp group does not match', () =>
request(server)
.get('/api/get?locale=sp')
.expect(200, { languageCode: 'sp', countryCode: 'US' }))

test('returns all data when a regexp group does match', () =>
request(server)
.get('/api/get?locale=sp-SP')
.expect(200, { languageCode: 'sp', countryCode: 'SP' }))
})

0 comments on commit 54d4d1f

Please sign in to comment.