0 points

JS Proxy Example


dashboard

Mon Jun 22 2020 09:42:01 GMT+0000 (UTC)

Posted by @ZivE #javascript

import uuid from 'uuid/v4'

export default function getTestIDs() {
  if (process.env.NODE_ENV !== 'test') return {}

  let ids = new Map()
  let proxy = new Proxy({}, {
    get: function(obj, prop) {
      if (ids.has(prop) === false) {
        ids.set(prop, uuid())
      }
      return ids.get(prop)
    }
  })
  return proxy
}
content_copy Copy

https://www.matthewsessions.com/blog/react-test-id/