Skip to content

Commit

Permalink
feat: test user subscribed events
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek committed Nov 5, 2024
1 parent c57708a commit 019eb98
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
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';
Expand All @@ -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',
);
Expand All @@ -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(
Expand All @@ -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',
},
}),
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Production updates last month: {{productionUpdates}}
Go to your Insights to learn more: {{unleashUrl}}/insights

This email was sent to {{userEmail}}. You’ve received this as you are a user of Unleash.
If you wish to unsubscribe, go to you profile settings <a href="{{unleashUrl}}/profile">here</a>.
If you wish to unsubscribe, go to you profile settings on {{unleashUrl}}/profile

0 comments on commit 019eb98

Please sign in to comment.