Skip to content

Commit

Permalink
Rename Collection.getById to `getByIdentity'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivandotv committed Jul 1, 2022
1 parent aeaa369 commit 464fa7d
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-lions-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fuerte/core': major
---

Rename `Collection.getById` to `getByIdentity'
10 changes: 5 additions & 5 deletions packages/core/src/__tests__/collection-add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('Collection - add #add #collection', () => {
const model = fixtures.model({ foo: 'foo', bar: 'bar', id: '1' })

collection.add(model)
const found = collection.getById(model.identity)
const found = collection.getByIdentity(model.identity)

expect(found).toBe(model)
})
Expand Down Expand Up @@ -233,11 +233,11 @@ describe('Collection - add #add #collection', () => {
const model = new Test()
collection.add(model)

expect(collection.getById(newIdentity)).toBeUndefined()
expect(collection.getByIdentity(newIdentity)).toBeUndefined()

model.setIdentity(newIdentity)

expect(collection.getById(newIdentity)).toBe(model)
expect(collection.getByIdentity(newIdentity)).toBe(model)
})

test('When model identity changes, get the model by new identity value', () => {
Expand All @@ -258,10 +258,10 @@ describe('Collection - add #add #collection', () => {
const model = new Test('1')
collection.add(model)

expect(collection.getById(model.identity)).toBe(model)
expect(collection.getByIdentity(model.identity)).toBe(model)

model.setIdentity(newIdentityValue)
expect(collection.getById(newIdentityValue)).toBe(model)
expect(collection.getByIdentity(newIdentityValue)).toBe(model)
})
})
})
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/collection-reset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ describe('Collection - reset #reset #collection', () => {
await collection.reset(fixtures.rawModelData)

expect(collection.models).toHaveLength(fixtures.rawModelData.length)
expect(collection.getById(models[0].cid)).toBeUndefined()
expect(collection.getById(models[1].cid)).toBeUndefined()
expect(collection.getByIdentity(models[0].cid)).toBeUndefined()
expect(collection.getByIdentity(models[1].cid)).toBeUndefined()
})

test('Reset the collection without adding new models', async () => {
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/__tests__/collection-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ describe('Collection - save #save #collection', () => {
const modelSaveErrorSpy = jest.spyOn(model, 'onSaveError')
jest.spyOn(transport, 'save').mockRejectedValue(response)

// @ts-expect-error collection config
const result = await collection.save(model, config, transportConfig)

expect(collectionSaveErrorSpy).toHaveBeenCalledWith({
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/__tests__/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('Collection #collection', () => {

collection.add(model)

const result = collection.getById(id)
const result = collection.getByIdentity(id)

expect(result).toBe(model)
})
Expand All @@ -140,7 +140,7 @@ describe('Collection #collection', () => {

collection.add([model, modelTwo])

const result = collection.getById([id, idTwo])
const result = collection.getByIdentity([id, idTwo])

expect(result).toStrictEqual([model, modelTwo])
})
Expand All @@ -149,7 +149,7 @@ describe('Collection #collection', () => {
const collection = fixtures.collection()
const id = 'new-id'

const result = collection.getById(id)
const result = collection.getByIdentity(id)

expect(result).toBeUndefined()
})
Expand All @@ -159,7 +159,7 @@ describe('Collection #collection', () => {
const id = 'new-id'
const idTwo = 'new-id-2'

const result = collection.getById([id, idTwo])
const result = collection.getByIdentity([id, idTwo])

expect(result).toStrictEqual([])
})
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/model-removed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Model - remove #remove #model', () => {

collection.remove(model.identity)

expect(collection.getById(model.identity)).toBeUndefined()
expect(collection.getByIdentity(model.identity)).toBeUndefined()
})

test('Destroy method is called by default', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Model #model', () => {

expect(transportSaveSpy).toHaveBeenCalledTimes(1)
expect(transportSaveSpy).toHaveBeenCalledWith(model, transportConfg)
expect(collection.getById(model.identity)).toBe(model)
expect(collection.getByIdentity(model.identity)).toBe(model)
})

test('calling model.save will throw if the model is not a part of the collection', async () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/collection/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,15 +871,15 @@ export class Collection<
return result as ReturnType<TFactory>
}

getById(id: string): TModel | undefined
getByIdentity(id: string): TModel | undefined

getById(id: string[]): TModel[] | undefined
getByIdentity(id: string[]): TModel[] | undefined

/**
* Get model or array of models from the collection by identity key, or model CID.
* @param id - identity key or CID
*/
getById(id: string | string[]): TModel | TModel[] | undefined {
getByIdentity(id: string | string[]): TModel | TModel[] | undefined {
if (Array.isArray(id)) {
return this.resolveModels(id)
}
Expand Down

0 comments on commit 464fa7d

Please sign in to comment.