-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Fix bug when instantiating entity from object with getter
Relates to #2574
- Loading branch information
1 parent
82ee913
commit d09452e
Showing
2 changed files
with
77 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { DeepPartial } from '@vendure/common/lib/shared-types'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { Calculated } from '../../common/index'; | ||
import { CalculatedPropertySubscriber } from '../subscribers'; | ||
|
||
import { VendureEntity } from './base.entity'; | ||
|
||
class ChildEntity extends VendureEntity { | ||
constructor(input?: DeepPartial<ChildEntity>) { | ||
super(input); | ||
} | ||
|
||
name: string; | ||
|
||
get nameLoud(): string { | ||
return this.name.toUpperCase(); | ||
} | ||
} | ||
|
||
class ChildEntityWithCalculated extends VendureEntity { | ||
constructor(input?: DeepPartial<ChildEntity>) { | ||
super(input); | ||
} | ||
|
||
name: string; | ||
|
||
@Calculated() | ||
get nameLoudCalculated(): string { | ||
return this.name.toUpperCase(); | ||
} | ||
} | ||
|
||
describe('VendureEntity', () => { | ||
it('instantiating a child entity', () => { | ||
const child = new ChildEntity({ | ||
name: 'foo', | ||
}); | ||
|
||
expect(child.name).toBe('foo'); | ||
expect(child.nameLoud).toBe('FOO'); | ||
}); | ||
|
||
it('instantiating from existing entity with getter', () => { | ||
const child1 = new ChildEntity({ | ||
name: 'foo', | ||
}); | ||
|
||
const child2 = new ChildEntity(child1); | ||
|
||
expect(child2.name).toBe('foo'); | ||
expect(child2.nameLoud).toBe('FOO'); | ||
}); | ||
|
||
it('instantiating from existing entity with calculated getter', () => { | ||
const calculatedPropertySubscriber = new CalculatedPropertySubscriber(); | ||
const child1 = new ChildEntityWithCalculated({ | ||
name: 'foo', | ||
}); | ||
|
||
// This is what happens to entities after being loaded from the DB | ||
calculatedPropertySubscriber.afterLoad(child1); | ||
|
||
const child2 = new ChildEntityWithCalculated(child1); | ||
|
||
expect(child2.name).toBe('foo'); | ||
expect(child2.nameLoudCalculated).toBe('FOO'); | ||
}); | ||
}); |
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