-
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.
Signed-off-by: Abdelrahman Essawy <[email protected]>
- Loading branch information
1 parent
883dd35
commit 9704f00
Showing
8 changed files
with
1,415 additions
and
26 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,8 @@ | ||
# Changesets | ||
|
||
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works | ||
with multi-package repos, or single-package repos to help you version and publish your code. You can | ||
find the full documentation for it [in our repository](https://github.com/changesets/changesets) | ||
|
||
We have a quick list of common questions to get you started engaging with this project in | ||
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) |
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,11 @@ | ||
{ | ||
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json", | ||
"changelog": "@changesets/cli/changelog", | ||
"commit": false, | ||
"fixed": [], | ||
"linked": [], | ||
"access": "restricted", | ||
"baseBranch": "main", | ||
"updateInternalDependencies": "patch", | ||
"ignore": [] | ||
} |
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,5 @@ | ||
--- | ||
'@enegix/source': minor | ||
--- | ||
|
||
allowing to subssribe to array of events |
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 |
---|---|---|
@@ -1,7 +1,80 @@ | ||
import { core } from './core'; | ||
// Mock console.log to prevent output during testing | ||
import { publish, subscribe, unsubscribeAll } from '@enegix/events'; | ||
import { expect, MockInstance, vitest } from 'vitest'; | ||
|
||
describe('core', () => { | ||
it('should work', () => { | ||
expect(core()).toEqual('core'); | ||
describe('subscribe function', () => { | ||
let callbackSpy: MockInstance; | ||
|
||
beforeEach(() => { | ||
callbackSpy = vitest.spyOn(console, 'log'); | ||
}); | ||
|
||
afterEach(() => { | ||
callbackSpy.mockRestore(); | ||
unsubscribeAll(); | ||
}); | ||
|
||
test('subscribing to a single event', () => { | ||
const callback = console.log; | ||
const { unsubscribe } = subscribe('event1', callback); | ||
|
||
publish('event1', 'testData'); | ||
|
||
expect(callbackSpy).toHaveBeenCalledWith('testData'); | ||
expect(callbackSpy).toHaveBeenCalledWith( | ||
'Published \'event1\' event with data: "testData"' | ||
); | ||
}); | ||
|
||
test('subscribing to multiple events', () => { | ||
const callback = console.log; | ||
const { unsubscribe } = subscribe(['event1', 'event2'], callback); | ||
|
||
publish('event1', 'testData1'); | ||
publish('event2', 'testData2'); | ||
|
||
expect(callbackSpy).toHaveBeenCalledWith('testData1'); | ||
expect(callbackSpy).toHaveBeenCalledWith('testData2'); | ||
expect(callbackSpy).toHaveBeenCalledWith( | ||
'Published \'event1\' event with data: "testData1"' | ||
); | ||
expect(callbackSpy).toHaveBeenCalledWith( | ||
'Published \'event2\' event with data: "testData2"' | ||
); | ||
}); | ||
|
||
test('unsubscribe from event', () => { | ||
const callback = console.log; | ||
const { unsubscribe } = subscribe('event1', callback); | ||
|
||
publish('event1', 'testData'); | ||
unsubscribe(); | ||
publish('event1', 'ignoreData'); | ||
|
||
expect(callbackSpy).toHaveBeenCalledWith( | ||
'Published \'event1\' event with data: "testData"' | ||
); | ||
expect(callbackSpy).toHaveBeenCalledWith('testData'); | ||
expect(callbackSpy).not.toHaveBeenCalledWith('ignoreData'); | ||
}); | ||
|
||
test('unsubscribe from multiple events', () => { | ||
const callback = console.log; | ||
const { unsubscribe } = subscribe(['event1', 'event2'], callback); | ||
|
||
publish('event1', 'testData1'); | ||
publish('event2', 'testData2'); | ||
unsubscribe(); | ||
publish('event1', 'testData1'); | ||
publish('event2', 'testData2'); | ||
|
||
expect(callbackSpy).toHaveBeenCalledWith('testData1'); | ||
expect(callbackSpy).toHaveBeenCalledWith('testData2'); | ||
expect(callbackSpy).toHaveBeenCalledWith( | ||
'Published \'event1\' event with data: "testData1"' | ||
); | ||
expect(callbackSpy).toHaveBeenCalledWith( | ||
'Published \'event2\' event with data: "testData2"' | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.