Skip to content

Commit fd86d75

Browse files
Hive. ListStore. Add support JSONValue to Method "set" (#232)
* Hive. ListStore. Add support JSONValue to Method "set" * Hive. ListStore. Add support JSONValue to Method "set" * Hive. Refactor SET method
1 parent 645be87 commit fd86d75

File tree

4 files changed

+138
-27
lines changed

4 files changed

+138
-27
lines changed

backendless.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,9 @@ declare module Backendless {
414414

415415
get(indexFrom: number, indexTo: number): Promise<Array<JSONValue>>
416416

417-
set(values: Array<string>): Promise<number>;
417+
set(values: Array<JSONValue>): Promise<number>;
418418

419-
set(value: string, index: number): Promise<void>;
419+
set(value: JSONValue, index: number): Promise<void>;
420420

421421
insertBefore(valueToInsert: JSONValue, anchorValue: JSONValue): Promise<number>;
422422

src/hive/stores/list.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,23 @@ export class ListStore extends HiveStore {
4141
}
4242

4343
set(value, index) {
44-
if (Array.isArray(value)) {
44+
if (typeof index === undefined) {
45+
if (!value || !Array.isArray(value) || !value.length || !isHiveValueValid(value)) {
46+
throw new Error('Value must be provided and must be a list of valid JSON items.')
47+
}
48+
4549
return this.app.request
4650
.put({
4751
url : this.getBaseURL(),
4852
data: value
4953
})
5054
}
5155

52-
if (isNaN(index) || typeof index !== 'number') {
56+
if (!isHiveValueValid(value)) {
57+
throw new Error('Value must be provided and must be one of types: string, number, boolean, object, array.')
58+
}
59+
60+
if (typeof index !== 'number' || isNaN(index)) {
5361
throw new Error('Index must be a number.')
5462
}
5563

@@ -79,7 +87,9 @@ export class ListStore extends HiveStore {
7987

8088
insert(valueToInsert, anchorValue, before) {
8189
if (!isHiveValueValid(valueToInsert)) {
82-
throw new Error('ValueToInsert must be provided and must be on of types: string, number, boolean, object, array.')
90+
throw new Error(
91+
'ValueToInsert must be provided and must be one of types: string, number, boolean, object, array.'
92+
)
8393
}
8494

8595
if (!isHiveValueValid(anchorValue)) {

test/tsd.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,16 @@ function testHiveStores() {
837837
promiseListOfJSONValue = Backendless.Hive(str).ListStore(str).get(num, num);
838838

839839
promiseNumber = Backendless.Hive(str).ListStore(str).set([str, str]);
840+
promiseNumber = Backendless.Hive(str).ListStore(str).set([num, str]);
841+
promiseNumber = Backendless.Hive(str).ListStore(str).set([bool, str]);
842+
promiseNumber = Backendless.Hive(str).ListStore(str).set([obj, str]);
843+
promiseNumber = Backendless.Hive(str).ListStore(str).set([strArr, str]);
844+
840845
promiseVoid = Backendless.Hive(str).ListStore(str).set(str, num);
846+
promiseVoid = Backendless.Hive(str).ListStore(str).set(num, num);
847+
promiseVoid = Backendless.Hive(str).ListStore(str).set(bool, num);
848+
promiseVoid = Backendless.Hive(str).ListStore(str).set(obj, num);
849+
promiseVoid = Backendless.Hive(str).ListStore(str).set(strArr, num);
841850

842851
promiseNumber = Backendless.Hive(str).ListStore(str).insertBefore(str, str);
843852
promiseNumber = Backendless.Hive(str).ListStore(str).insertBefore(num, num);

test/unit/specs/hive/list.js

Lines changed: 114 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -536,33 +536,123 @@ describe('Hive - List Store', function() {
536536

537537
describe('Set', async () => {
538538
it('success', async () => {
539-
const request = prepareMockRequest(fakeResult)
539+
const composeRequest = async value => {
540+
const request = prepareMockRequest(fakeResult)
540541

541-
const result = await store.set(['value1', 'value2'])
542+
const result = await store.set(value)
542543

543-
expect(request).to.deep.include({
544-
method: 'PUT',
545-
path : `${APP_PATH}/hive/${hiveName}/list/${storeKey}`,
546-
body : ['value1', 'value2']
547-
})
544+
const payload = {
545+
method: 'PUT',
546+
path : `${APP_PATH}/hive/${hiveName}/list/${storeKey}`,
547+
body : value
548+
}
548549

549-
expect(result).to.be.eql(fakeResult)
550+
return { request, result, payload }
551+
}
552+
553+
const request1 = await composeRequest(['string'])
554+
const request2 = await composeRequest([''])
555+
const request3 = await composeRequest([false])
556+
const request4 = await composeRequest([true])
557+
const request5 = await composeRequest([[]])
558+
const request6 = await composeRequest([123])
559+
const request7 = await composeRequest([{ a: 1 }])
560+
561+
expect(request1.request).to.deep.include(request1.payload)
562+
expect(request2.request).to.deep.include(request2.payload)
563+
expect(request3.request).to.deep.include(request3.payload)
564+
expect(request4.request).to.deep.include(request4.payload)
565+
expect(request5.request).to.deep.include(request5.payload)
566+
expect(request6.request).to.deep.include(request6.payload)
567+
expect(request7.request).to.deep.include(request7.payload)
568+
569+
expect(request1.result).to.be.eql(fakeResult)
570+
expect(request2.result).to.be.eql(fakeResult)
571+
expect(request3.result).to.be.eql(fakeResult)
572+
expect(request4.result).to.be.eql(fakeResult)
573+
expect(request5.result).to.be.eql(fakeResult)
574+
expect(request6.result).to.be.eql(fakeResult)
575+
expect(request7.result).to.be.eql(fakeResult)
550576
})
551577

552-
it('success with index', async () => {
553-
const request = prepareMockRequest(fakeResult)
578+
it('success values with index', async () => {
579+
const composeRequest = async value => {
580+
const request = prepareMockRequest(fakeResult)
554581

555-
const result = await store.set('value1', 0)
582+
const result = await store.set(value, 0)
556583

557-
expect(request).to.deep.include({
558-
method: 'PUT',
559-
path : `${APP_PATH}/hive/${hiveName}/list/${storeKey}/0`,
560-
body : {
561-
value: 'value1'
584+
const payload = {
585+
method: 'PUT',
586+
path : `${APP_PATH}/hive/${hiveName}/list/${storeKey}/0`,
587+
body : {
588+
value: value
589+
}
562590
}
563-
})
564591

565-
expect(result).to.be.eql(fakeResult)
592+
return { request, result, payload }
593+
}
594+
595+
const request1 = await composeRequest('string')
596+
const request2 = await composeRequest('')
597+
const request3 = await composeRequest(false)
598+
const request4 = await composeRequest(true)
599+
const request5 = await composeRequest([])
600+
const request6 = await composeRequest(123)
601+
const request7 = await composeRequest(0)
602+
const request8 = await composeRequest({ a: 1 })
603+
604+
expect(request1.request).to.deep.include(request1.payload)
605+
expect(request2.request).to.deep.include(request2.payload)
606+
expect(request3.request).to.deep.include(request3.payload)
607+
expect(request4.request).to.deep.include(request4.payload)
608+
expect(request5.request).to.deep.include(request5.payload)
609+
expect(request6.request).to.deep.include(request6.payload)
610+
expect(request7.request).to.deep.include(request7.payload)
611+
expect(request8.request).to.deep.include(request8.payload)
612+
613+
expect(request1.result).to.be.eql(fakeResult)
614+
expect(request2.result).to.be.eql(fakeResult)
615+
expect(request3.result).to.be.eql(fakeResult)
616+
expect(request4.result).to.be.eql(fakeResult)
617+
expect(request5.result).to.be.eql(fakeResult)
618+
expect(request6.result).to.be.eql(fakeResult)
619+
expect(request7.result).to.be.eql(fakeResult)
620+
expect(request8.result).to.be.eql(fakeResult)
621+
})
622+
623+
it('fails with invalid value', async () => {
624+
store = Backendless.Hive(hiveName).ListStore(storeKey)
625+
626+
const errorMsg = 'Value must be provided and must be a list of valid JSON items.'
627+
628+
await expect(() => store.set(undefined)).to.throw(errorMsg)
629+
await expect(() => store.set(null)).to.throw(errorMsg)
630+
await expect(() => store.set([])).to.throw(errorMsg)
631+
await expect(() => store.set('string')).to.throw(errorMsg)
632+
await expect(() => store.set('')).to.throw(errorMsg)
633+
await expect(() => store.set(0)).to.throw(errorMsg)
634+
await expect(() => store.set(false)).to.throw(errorMsg)
635+
await expect(() => store.set('')).to.throw(errorMsg)
636+
await expect(() => store.set(true)).to.throw(errorMsg)
637+
await expect(() => store.set(123)).to.throw(errorMsg)
638+
await expect(() => store.set(() => undefined)).to.throw(errorMsg)
639+
await expect(() => store.set({})).to.throw(errorMsg)
640+
await expect(() => store.set(Symbol('id'))).to.throw(errorMsg)
641+
await expect(() => store.set(10n)).to.throw(errorMsg)
642+
await expect(() => store.set([10n])).to.throw(errorMsg)
643+
})
644+
645+
it('fails with index', async () => {
646+
store = Backendless.Hive(hiveName).ListStore(storeKey)
647+
648+
const errorMsg = 'Value must be provided and must be one of types: string, number, boolean, object, array.'
649+
650+
await expect(() => store.set(undefined, 0)).to.throw(errorMsg)
651+
await expect(() => store.set(null, 0)).to.throw(errorMsg)
652+
await expect(() => store.set(() => true, 0)).to.throw(errorMsg)
653+
await expect(() => store.set(10n, 0)).to.throw(errorMsg)
654+
await expect(() => store.set(Symbol('id'), 0)).to.throw(errorMsg)
655+
await expect(() => store.set([10n], 0)).to.throw(errorMsg)
566656
})
567657

568658
it('fails with invalid index', async () => {
@@ -573,11 +663,14 @@ describe('Hive - List Store', function() {
573663
await expect(() => store.set('v', null)).to.throw(errorMsg)
574664
await expect(() => store.set('v', NaN)).to.throw(errorMsg)
575665
await expect(() => store.set('v', false)).to.throw(errorMsg)
666+
await expect(() => store.set('v', true)).to.throw(errorMsg)
576667
await expect(() => store.set('v', '')).to.throw(errorMsg)
577668
await expect(() => store.set('v', 'qwe')).to.throw(errorMsg)
578-
await expect(() => store.set('v', true)).to.throw(errorMsg)
669+
await expect(() => store.set('v', 10n)).to.throw(errorMsg)
670+
await expect(() => store.set('v', Symbol('id'))).to.throw(errorMsg)
579671
await expect(() => store.set('v', () => undefined)).to.throw(errorMsg)
580672
await expect(() => store.set('v', {})).to.throw(errorMsg)
673+
await expect(() => store.set('v', [])).to.throw(errorMsg)
581674
})
582675
})
583676

@@ -693,7 +786,7 @@ describe('Hive - List Store', function() {
693786
})
694787

695788
it('fails with invalid ValueToInsert', async () => {
696-
const errorMsg = 'ValueToInsert must be provided and must be on of types: string, number, boolean, object, array.'
789+
const errorMsg = 'ValueToInsert must be provided and must be one of types: string, number, boolean, object, array.'
697790

698791
await expect(() => store.insertBefore(undefined, 'v')).to.throw(errorMsg)
699792
await expect(() => store.insertBefore(null, 'v')).to.throw(errorMsg)
@@ -811,7 +904,7 @@ describe('Hive - List Store', function() {
811904
})
812905

813906
it('fails with invalid ValueToInsert', async () => {
814-
const errorMsg = 'ValueToInsert must be provided and must be on of types: string, number, boolean, object, array.'
907+
const errorMsg = 'ValueToInsert must be provided and must be one of types: string, number, boolean, object, array.'
815908

816909
await expect(() => store.insertAfter(undefined, 'v')).to.throw(errorMsg)
817910
await expect(() => store.insertAfter(null, 'v')).to.throw(errorMsg)
@@ -1035,7 +1128,6 @@ describe('Hive - List Store', function() {
10351128
await expect(() => store.addFirstValues({})).to.throw(errorMsg)
10361129
await expect(() => store.addFirstValues(Symbol('id'))).to.throw(errorMsg)
10371130
await expect(() => store.addFirstValues(10n)).to.throw(errorMsg)
1038-
await expect(() => store.addFirstValues([])).to.throw(errorMsg)
10391131
await expect(() => store.addFirstValues([10n])).to.throw(errorMsg)
10401132
})
10411133
})

0 commit comments

Comments
 (0)