-
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.
test: add unit tests for
setReadonlyConstantToGlobalThis
- Loading branch information
1 parent
ca225a5
commit aafe978
Showing
1 changed file
with
32 additions
and
0 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,32 @@ | ||
import { setReadonlyConstantToGlobalThis } from '../src/object'; | ||
|
||
describe('setReadonlyConstantToGlobalThis', () => { | ||
const constantValue = 'testValue'; | ||
it('should define a readonly constant on globalThis', () => { | ||
const constantName = Math.random().toString(36).substring(2, 15); | ||
setReadonlyConstantToGlobalThis(constantName, constantValue); | ||
expect((globalThis as any)[constantName]).toBe(constantValue); | ||
expect(() => ((globalThis as any)[constantName] = 'newValue')).toThrow(TypeError); | ||
expect((globalThis as any)[constantName]).toBe(constantValue); | ||
}); | ||
|
||
it('should not be configurable', () => { | ||
const constantName = Math.random().toString(36).substring(2, 15); | ||
setReadonlyConstantToGlobalThis(constantName, constantValue); | ||
const deleteResult = delete (globalThis as any)[constantName]; | ||
expect(deleteResult).toBe(false); | ||
expect((globalThis as any)[constantName]).toBe(constantValue); | ||
}); | ||
|
||
it('should allow passing additional attributes', () => { | ||
const constantName = Math.random().toString(36).substring(2, 15); | ||
setReadonlyConstantToGlobalThis(constantName, constantValue, { enumerable: true }); | ||
expect(Object.getOwnPropertyDescriptor(globalThis, constantName)?.enumerable).toBe(true); | ||
}); | ||
|
||
it('should not be writable', () => { | ||
const constantName = Math.random().toString(36).substring(2, 15); | ||
setReadonlyConstantToGlobalThis(constantName, constantValue); | ||
expect(Object.getOwnPropertyDescriptor(globalThis, constantName)?.writable).toBe(false); | ||
}); | ||
}); |