• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ecamp / hal-json-vuex / 13321877237

13 Feb 2025 10:43PM UTC coverage: 84.708%. Remained the same
13321877237

push

github

web-flow
Update babel monorepo to v7.26.8

159 of 206 branches covered (77.18%)

Branch coverage included in aggregate %.

262 of 291 relevant lines covered (90.03%)

740.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/src/CanHaveItems.js
1
import { isEntityReference } from './halHelpers.js'
2
import LoadingStoreCollection from './LoadingStoreCollection'
3

4
class CanHaveItems {
5
  constructor ({ get, reload, isUnknown }, config) {
6
    this.apiActions = { get, reload, isUnknown }
1,545✔
7
    this.config = config
1,545✔
8
  }
9

10
  /**
11
     * Defines a property getter for the items property.
12
     * The items property should always be a getter, in order to make the call to mapArrayOfEntityReferences
13
     * lazy, since that potentially fetches a large number of entities from the API.
14
     * @param items       array of items, which can be mixed primitive values and entity references
15
     * @param fetchAllUri URI that allows fetching all collection items in a single network request, if known
16
     * @param property    property name inside the entity fetched at fetchAllUri that contains the collection
17
     * @returns object the target object with the added getter
18
     */
19
  addItemsGetter (items, fetchAllUri, property) {
20
    Object.defineProperty(this, 'items', { get: () => this.filterDeleting(this.mapArrayOfEntityReferences(items, fetchAllUri, property)) })
276✔
21
    Object.defineProperty(this, 'allItems', { get: () => this.mapArrayOfEntityReferences(items, fetchAllUri, property) })
276✔
22
  }
23

24
  filterDeleting (array) {
25
    return array.filter(entry => !entry._meta.deleting)
255✔
26
  }
27

28
  /**
29
     * Given an array, replaces any entity references in the array with the entity loaded from the Vuex store
30
     * (or from the API if necessary), and returns that as a new array. In case some of the entity references in
31
     * the array have not finished loading yet, returns a LoadingStoreCollection instead.
32
     * @param array            possibly mixed array of values and references
33
     * @param fetchAllUri      URI that allows fetching all array items in a single network request, if known
34
     * @param fetchAllProperty property in the entity from fetchAllUri that will contain the array
35
     * @returns array          the new array with replaced items, or a LoadingStoreCollection if any of the array
36
     *                         elements is still loading.
37
     */
38
  mapArrayOfEntityReferences (array, fetchAllUri, fetchAllProperty) {
39
    if (!this.containsUnknownEntityReference(array)) {
153✔
40
      return this.replaceEntityReferences(array)
138✔
41
    }
42

43
    if (this.config.avoidNPlusOneRequests) {
15✔
44
      const completelyLoaded = this.apiActions.reload({ _meta: { reload: { uri: fetchAllUri, property: fetchAllProperty } } }, true)
12✔
45
        .then(() => this.replaceEntityReferences(array))
12✔
46
      return new LoadingStoreCollection(completelyLoaded)
12✔
47
    } else {
48
      const arrayWithReplacedReferences = this.replaceEntityReferences(array)
3✔
49
      const arrayCompletelyLoaded = Promise.all(array.map(entry => {
3✔
50
        if (isEntityReference(entry)) {
9!
51
          return this.apiActions.get(entry.href)._meta.load
9✔
52
        }
53
        return Promise.resolve(entry)
×
54
      }))
55
      return new LoadingStoreCollection(arrayCompletelyLoaded, arrayWithReplacedReferences)
3✔
56
    }
57
  }
58

59
  replaceEntityReferences (array) {
60
    return array.map(entry => {
153✔
61
      if (isEntityReference(entry)) {
303!
62
        return this.apiActions.get(entry.href)
303✔
63
      }
64
      return entry
×
65
    })
66
  }
67

68
  containsUnknownEntityReference (array) {
69
    return array.some(entry => isEntityReference(entry) && this.apiActions.isUnknown(entry.href))
273✔
70
  }
71
}
72

73
export default CanHaveItems
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc