-
-
Notifications
You must be signed in to change notification settings - Fork 730
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
2 changed files
with
60 additions
and
9 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { | ||
type IEventStore, | ||
type IUnleashConfig, | ||
type IUnleashStores, | ||
type IUser, | ||
type IUserStore, | ||
TEST_AUDIT_USER, | ||
} from '../../types'; | ||
import type { UserSubscriptionsService } from './user-subscriptions-service'; | ||
|
@@ -13,32 +14,39 @@ import type { IUserSubscriptionsReadModel } from './user-subscriptions-read-mode | |
|
||
let stores: IUnleashStores; | ||
let db: ITestDb; | ||
let userStore: IUserStore; | ||
let userSubscriptionService: UserSubscriptionsService; | ||
let userSubscriptionsReadModel: IUserSubscriptionsReadModel; | ||
let eventsStore: IEventStore; | ||
let config: IUnleashConfig; | ||
let user: IUser; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('user_subscriptions', getLogger); | ||
stores = db.stores; | ||
config = createTestConfig({}); | ||
|
||
userStore = stores.userStore; | ||
userSubscriptionService = createUserSubscriptionsService(config)( | ||
db.rawDatabase, | ||
); | ||
userSubscriptionsReadModel = db.stores.userSubscriptionsReadModel; | ||
eventsStore = db.stores.eventStore; | ||
}); | ||
|
||
user = await stores.userStore.insert({ | ||
email: '[email protected]', | ||
name: 'Sample Name', | ||
}); | ||
beforeEach(async () => { | ||
await userStore.deleteAll(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.destroy(); | ||
}); | ||
|
||
test('Subscribe and unsubscribe', async () => { | ||
const user = await userStore.insert({ | ||
email: '[email protected]', | ||
name: 'Sample Name', | ||
}); | ||
|
||
const subscribers = await userSubscriptionsReadModel.getSubscribedUsers( | ||
'productivity-report', | ||
); | ||
|
@@ -47,7 +55,7 @@ test('Subscribe and unsubscribe', async () => { | |
]); | ||
|
||
const userSubscriptions = | ||
await userSubscriptionsReadModel.getUserSubscriptions(user.id); | ||
await userSubscriptionService.getUserSubscriptions(user.id); | ||
expect(userSubscriptions).toMatchObject(['productivity-report']); | ||
|
||
await userSubscriptionService.unsubscribe( | ||
|
@@ -62,6 +70,49 @@ test('Subscribe and unsubscribe', async () => { | |
expect(noSubscribers).toMatchObject([]); | ||
|
||
const noUserSubscriptions = | ||
await userSubscriptionsReadModel.getUserSubscriptions(user.id); | ||
await userSubscriptionService.getUserSubscriptions(user.id); | ||
expect(noUserSubscriptions).toMatchObject([]); | ||
}); | ||
|
||
test('Event log for subscription actions', async () => { | ||
const user = await userStore.insert({ | ||
email: '[email protected]', | ||
name: 'Sample Name', | ||
}); | ||
|
||
await userSubscriptionService.unsubscribe( | ||
user.id, | ||
'productivity-report', | ||
TEST_AUDIT_USER, | ||
); | ||
|
||
const unsubscribeEvent = (await eventsStore.getAll())[0]; | ||
|
||
expect(unsubscribeEvent).toEqual( | ||
expect.objectContaining({ | ||
type: 'user-preference-updated', | ||
data: { | ||
subscription: 'productivity-report', | ||
action: 'unsubscribed', | ||
}, | ||
}), | ||
); | ||
|
||
await userSubscriptionService.subscribe( | ||
user.id, | ||
'productivity-report', | ||
TEST_AUDIT_USER, | ||
); | ||
|
||
const subscribeEvent = (await eventsStore.getAll())[0]; | ||
|
||
expect(subscribeEvent).toEqual( | ||
expect.objectContaining({ | ||
type: 'user-preference-updated', | ||
data: { | ||
subscription: 'productivity-report', | ||
action: 'subscribed', | ||
}, | ||
}), | ||
); | ||
}); |
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