-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(pinia-orm): Model original data is overwritten by different namsp…
…ace models with same ID (#1954) * Model original data isolation * Fix linting issues in added tests. resolves #1954
- Loading branch information
Showing
2 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/pinia-orm/tests/unit/model/Model_Isolation.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { Model } from '../../../src' | ||
import { Attr } from '../../../src/decorators' | ||
|
||
describe('unit/model/ModelIsolation', () => { | ||
class User extends Model { | ||
static entity = 'users' | ||
@Attr() declare id: number | ||
@Attr() declare name: string | ||
} | ||
|
||
class Post extends Model { | ||
static entity = 'posts' | ||
@Attr() declare id: number | ||
@Attr() declare title: string | ||
} | ||
|
||
it('maintains separate original states for different model types with same id', () => { | ||
const user = new User({ id: 1, name: 'John' }) | ||
const post = new Post({ id: 1, title: 'Hello' }) | ||
|
||
expect(user.$getOriginal()).toEqual({ id: 1, name: 'John' }) | ||
expect(post.$getOriginal()).toEqual({ id: 1, title: 'Hello' }) | ||
}) | ||
|
||
it('tracks changes independently for different model types', () => { | ||
const user = new User({ id: 1, name: 'John' }) | ||
const post = new Post({ id: 1, title: 'Hello' }) | ||
|
||
user.name = 'Jane' | ||
post.title = 'Updated' | ||
|
||
expect(user.$isDirty()).toBeTruthy() | ||
expect(post.$isDirty()).toBeTruthy() | ||
expect(user.$getOriginal()).toEqual({ id: 1, name: 'John' }) | ||
expect(post.$getOriginal()).toEqual({ id: 1, title: 'Hello' }) | ||
}) | ||
|
||
it('refreshes to correct original state for each model type', () => { | ||
const user = new User({ id: 1, name: 'John' }) | ||
const post = new Post({ id: 1, title: 'Hello' }) | ||
|
||
user.name = 'Jane' | ||
post.title = 'Updated' | ||
|
||
user.$refresh() | ||
expect(user.name).toBe('John') | ||
expect(post.title).toBe('Updated') | ||
|
||
post.$refresh() | ||
expect(post.title).toBe('Hello') | ||
}) | ||
|
||
it('handles multiple instances of different models with same ids', () => { | ||
const users = [ | ||
new User({ id: 1, name: 'John' }), | ||
new User({ id: 2, name: 'Jane' }), | ||
] | ||
|
||
const posts = [ | ||
new Post({ id: 1, title: 'First' }), | ||
new Post({ id: 2, title: 'Second' }), | ||
] | ||
|
||
users.forEach(u => u.name = `Updated ${u.name}`) | ||
posts.forEach(p => p.title = `Updated ${p.title}`) | ||
|
||
expect(users[0].$getOriginal()).toEqual({ id: 1, name: 'John' }) | ||
expect(users[1].$getOriginal()).toEqual({ id: 2, name: 'Jane' }) | ||
expect(posts[0].$getOriginal()).toEqual({ id: 1, title: 'First' }) | ||
expect(posts[1].$getOriginal()).toEqual({ id: 2, title: 'Second' }) | ||
}) | ||
}) |