-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
18 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
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
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
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,87 @@ | ||
import { Singleton } from 'src'; | ||
import { test, expect } from 'vitest'; | ||
|
||
test('should return the same instance when attempting to create multiple instances', () => { | ||
@Singleton | ||
class Foo { | ||
value: string; | ||
|
||
constructor(value: string) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
const foo1 = new Foo('first'); | ||
const foo2 = new Foo('second'); | ||
|
||
expect(foo1).toBe(foo2); | ||
expect(foo1.value).toBe('first'); | ||
}); | ||
|
||
test('should have no problem with instantiation', () => { | ||
@Singleton | ||
class Foo { | ||
value: string; | ||
|
||
constructor(value: string) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
expect(() => { | ||
new Foo('value'); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('should ensure singleton across different invocations', () => { | ||
@Singleton | ||
class Foo { | ||
value: string; | ||
|
||
constructor(value: string) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
const foo1 = new Foo('foo'); | ||
const foo2 = new Foo('bar'); | ||
|
||
expect(foo1).toBe(foo2); | ||
expect(foo1.value).toBe('foo'); | ||
}); | ||
|
||
test('should not allow overriding the singleton instance', () => { | ||
@Singleton | ||
class Foo { | ||
value: string; | ||
|
||
constructor(value: string) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
const foo1 = new Foo('initial'); | ||
const foo2 = new Foo('changed'); | ||
|
||
expect(foo1).toBe(foo2); | ||
expect(foo2.value).toBe('initial'); | ||
}); | ||
|
||
test('should return the same instance even after multiple constructor calls', () => { | ||
@Singleton | ||
class Foo { | ||
value: string; | ||
|
||
constructor(value: string) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
const foo1 = new Foo('first'); | ||
const foo2 = new Foo('second'); | ||
const foo3 = new Foo('third'); | ||
|
||
expect(foo1).toBe(foo2); | ||
expect(foo1).toBe(foo3); | ||
expect(foo3.value).toBe('first'); | ||
}); |
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