objects.test.ts 926 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { describe, expect, it } from 'vitest'
  2. import { entriesOf, getProp } from '..'
  3. const AXIOM = 'Rem is the best girl'
  4. describe('objects', () => {
  5. it('getProp should work', () => {
  6. const obj = {
  7. a: {
  8. b: {
  9. c: 'd',
  10. },
  11. },
  12. foo: {
  13. ['@@::']: 'hello',
  14. 'abc.': 'cde',
  15. },
  16. key: 'value',
  17. }
  18. // get
  19. expect(getProp(obj, 'a.b.c').value).toBe('d')
  20. expect(getProp(obj, 'key').value).toBe('value')
  21. expect(getProp(obj, 'foo.@@::').value).toBe('hello')
  22. expect(getProp(obj, ['foo', 'abc.']).value).toBe('cde')
  23. // set
  24. getProp(obj, ['foo', 'abc.']).value = AXIOM
  25. expect(obj.foo['abc.']).toBe(AXIOM)
  26. getProp(obj, 'a.b.c').value = AXIOM
  27. expect(obj.a.b.c).toBe(AXIOM)
  28. })
  29. it('entriesOf should work', () => {
  30. const obj = { key: 'value', foo: 'bar' }
  31. expect(entriesOf(obj)).toEqual(Object.entries(obj))
  32. })
  33. })