Skip to content

Commit

Permalink
fix(d): make init work multiple times (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Jan 5, 2024
1 parent ea78ee7 commit 71e341b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/composables/D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export interface D<T extends Record<string, any>> {
}

export function useD<T extends Record<string, any>>(data: T): D<T> {
const initialData = JSON.parse(JSON.stringify(data))
const initialData = JSON.stringify(data)

const refData = ref(data) as Ref<T>

function init(): void {
refData.value = initialData
refData.value = JSON.parse(initialData)
}

return {
Expand Down
12 changes: 12 additions & 0 deletions tests/composables/D.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,17 @@ describe('composables/D', () => {

expect(data.value.a).toBe(1)
expect(data.value.b).toBe(2)

// Check 2nd time reset to ensure it can reset the state multiple times.
data.value.a = 3
data.value.b = 4

expect(data.value.a).toBe(3)
expect(data.value.b).toBe(4)

init()

expect(data.value.a).toBe(1)
expect(data.value.b).toBe(2)
})
})

0 comments on commit 71e341b

Please sign in to comment.