repository.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { Repository } from '@vuex-orm/core'
  2. import * as _ from 'lodash'
  3. import { createStore } from '~/tests/unit/Helpers'
  4. import User from '~/tests/unit/fixture/models/User'
  5. import { repositoryHelper } from '~/services/store/repository'
  6. import { AnyStore } from '~/types/interfaces'
  7. let store: AnyStore, repository: Repository<User>
  8. beforeEach(() => {
  9. store = createStore()
  10. repository = store.$repo(User)
  11. repositoryHelper.setStore(store)
  12. })
  13. describe('getRepository()', () => {
  14. it('should return the repository of the Model', () => {
  15. expect(repositoryHelper.getRepository(User)).toStrictEqual(repository)
  16. })
  17. })
  18. describe('getEntity()', () => {
  19. it('should return the entity of the Model', () => {
  20. expect(repositoryHelper.getEntity(User)).toEqual('users')
  21. })
  22. })
  23. describe('updateStoreFromField()', () => {
  24. it('should throw an error if the field doest exist', () => {
  25. expect(() => repositoryHelper.updateStoreFromField(User, {}, 'Foo Bar', 'name')).toThrowError('field not found')
  26. })
  27. it('should update the store', () => {
  28. const user = repository.make()
  29. repository.save(user)
  30. repositoryHelper.updateStoreFromField(User, user.$toJson(), 'Foo Bar', 'name')
  31. const userUpdate = repository.find(1)
  32. expect(userUpdate?.$toJson()).toStrictEqual({ id: 1, name: 'Foo Bar' })
  33. })
  34. })
  35. describe('persist()', () => {
  36. it('should throw an error if the entry is empty', () => {
  37. expect(() => repositoryHelper.persist(User, {})).toThrowError()
  38. })
  39. it('should persist the entry inside the store', () => {
  40. const entryExpected = { id: 2, name: 'Alice' }
  41. repositoryHelper.persist(User, entryExpected)
  42. const alice = repository.find(2)
  43. expect(alice?.$toJson()).toStrictEqual(entryExpected)
  44. })
  45. })
  46. describe('findItemFromModel()', () => {
  47. it('should throw an error if the Item is not found', () => {
  48. expect(() => repositoryHelper.findItemFromModel(User, 1)).toThrowError('Item not found')
  49. })
  50. it('should return the correct item of the Model', () => {
  51. const user = repository.make()
  52. repository.save(user)
  53. expect(repositoryHelper.findItemFromModel(User, 1)).toStrictEqual(user)
  54. })
  55. })
  56. describe('findCollectionFromModel()', () => {
  57. it('should return the correct collection of the Model', () => {
  58. const UsersArray: Array<User> = []
  59. const user = repository.make()
  60. UsersArray.push(user)
  61. repository.save(user)
  62. const user2 = _.cloneDeep(user)
  63. user2.id = 2
  64. user2.name = 'Foo Bar'
  65. UsersArray.push(user2)
  66. repository.save(user2)
  67. expect(repositoryHelper.findCollectionFromModel(User)).toStrictEqual(UsersArray)
  68. })
  69. })
  70. describe('cleanRepository()', () => {
  71. it('should clean the entire repository', () => {
  72. const user = repository.make()
  73. repository.save(user)
  74. repositoryHelper.cleanRepository(User)
  75. expect(repository.all()).toEqual([])
  76. })
  77. })