utils.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  3. const parents = new WeakMap()
  4. const generatedIds = new Map<string, number>()
  5. afterEach(() => {
  6. generatedIds.clear()
  7. })
  8. export const getNodeParent = (node: any): any => {
  9. return parents.get(node)
  10. }
  11. export const setNodeParent = (node: any, parent: any): void => {
  12. parents.set(node, parent)
  13. }
  14. export const hasNodeParent = (node: any): boolean => {
  15. return parents.has(node)
  16. }
  17. export const generateGraphqlMockId = (parent: any): string => {
  18. const typename = parent.__typename
  19. const id = generatedIds.get(typename) || 0
  20. const newId = id + 1
  21. if (newId >= 100) {
  22. console.error(parent)
  23. throw new Error(
  24. `Detected a loop. Too many generated ids for ${typename} inside a single test.`,
  25. )
  26. }
  27. generatedIds.set(typename, newId)
  28. return convertToGraphQLId(typename, newId)
  29. }
  30. export const updateGeneratedIds = (typename: string, id: number): void => {
  31. const currentId = generatedIds.get(typename)
  32. if (currentId === undefined || id > currentId) {
  33. generatedIds.set(typename, id)
  34. }
  35. }