Skip to content

Commit

Permalink
test(react-store,vue-store): add test for shallow function on both re…
Browse files Browse the repository at this point in the history
…act/vue stores (#27)
  • Loading branch information
hyanmandian committed Oct 18, 2023
1 parent cfda5c5 commit 6989de5
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
55 changes: 54 additions & 1 deletion packages/react-store/src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, waitFor } from '@testing-library/react'
import '@testing-library/jest-dom'
import * as React from 'react'
import { Store } from '@tanstack/store'
import { useStore } from '../index'
import { useStore, shallow } from '../index'
import { useState } from 'react'
import userEvent from '@testing-library/user-event'

Expand Down Expand Up @@ -77,3 +77,56 @@ describe('useStore', () => {
expect(getByText('Number rendered: 2')).toBeInTheDocument()
})
})

describe('shallow', () => {
test('should return true for shallowly equal objects', () => {
const objA = { a: 1, b: 'hello' }
const objB = { a: 1, b: 'hello' }
expect(shallow(objA, objB)).toBe(true)
})

test('should return false for objects with different values', () => {
const objA = { a: 1, b: 'hello' }
const objB = { a: 2, b: 'world' }
expect(shallow(objA, objB)).toBe(false)
})

test('should return false for objects with different keys', () => {
const objA = { a: 1, b: 'hello' }
const objB = { a: 1, c: 'world' }
// @ts-ignore
expect(shallow(objA, objB)).toBe(false)
})

test('should return false for objects with different structures', () => {
const objA = { a: 1, b: 'hello' }
const objB = [1, 'hello']
// @ts-ignore
expect(shallow(objA, objB)).toBe(false)
})

test('should return false for one object being null', () => {
const objA = { a: 1, b: 'hello' }
const objB = null
expect(shallow(objA, objB)).toBe(false)
})

test('should return false for one object being undefined', () => {
const objA = { a: 1, b: 'hello' }
const objB = undefined
expect(shallow(objA, objB)).toBe(false)
})

test('should return true for two null objects', () => {
const objA = null
const objB = null
expect(shallow(objA, objB)).toBe(true)
})

test('should return false for objects with different types', () => {
const objA = { a: 1, b: 'hello' }
const objB = { a: '1', b: 'hello' }
// @ts-ignore
expect(shallow(objA, objB)).toBe(false)
})
})
55 changes: 54 additions & 1 deletion packages/vue-store/src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { h, defineComponent, watch } from 'vue-demi'
import { render, waitFor } from '@testing-library/vue'
import '@testing-library/jest-dom'
import { Store } from '@tanstack/store'
import { useStore } from '../index'
import { useStore, shallow } from '../index'
import userEvent from '@testing-library/user-event'

const user = userEvent.setup()
Expand Down Expand Up @@ -79,3 +79,56 @@ describe('useStore', () => {
expect(getByText('Number rendered: 2')).toBeInTheDocument()
})
})

describe('shallow', () => {
test('should return true for shallowly equal objects', () => {
const objA = { a: 1, b: 'hello' }
const objB = { a: 1, b: 'hello' }
expect(shallow(objA, objB)).toBe(true)
})

test('should return false for objects with different values', () => {
const objA = { a: 1, b: 'hello' }
const objB = { a: 2, b: 'world' }
expect(shallow(objA, objB)).toBe(false)
})

test('should return false for objects with different keys', () => {
const objA = { a: 1, b: 'hello' }
const objB = { a: 1, c: 'world' }
// @ts-ignore
expect(shallow(objA, objB)).toBe(false)
})

test('should return false for objects with different structures', () => {
const objA = { a: 1, b: 'hello' }
const objB = [1, 'hello']
// @ts-ignore
expect(shallow(objA, objB)).toBe(false)
})

test('should return false for one object being null', () => {
const objA = { a: 1, b: 'hello' }
const objB = null
expect(shallow(objA, objB)).toBe(false)
})

test('should return false for one object being undefined', () => {
const objA = { a: 1, b: 'hello' }
const objB = undefined
expect(shallow(objA, objB)).toBe(false)
})

test('should return true for two null objects', () => {
const objA = null
const objB = null
expect(shallow(objA, objB)).toBe(true)
})

test('should return false for objects with different types', () => {
const objA = { a: 1, b: 'hello' }
const objB = { a: '1', b: 'hello' }
// @ts-ignore
expect(shallow(objA, objB)).toBe(false)
})
})

0 comments on commit 6989de5

Please sign in to comment.