Skip to content

Commit

Permalink
fix: settings-disappear-on-ui-navigation
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Kumar <abhinav@avitechlab.com>
  • Loading branch information
abhinavkrin committed Jul 2, 2024
1 parent 7a57f34 commit 96bb00f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<
}

const { _id } = newRecord;
this.collection.upsert({ _id } as Mongo.Selector<T>, newRecord);
this.collection.update({ _id } as Mongo.Selector<T>, { $set: newRecord } as unknown as Mongo.Modifier<T>, { upsert: true });
this.emit('changed', newRecord as any); // TODO: investigate why this is needed

if (hasUpdatedAt(newRecord) && newRecord._updatedAt > this.updatedAt) {
Expand Down Expand Up @@ -250,7 +250,7 @@ export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<
if (!_id) {
return;
}
this.collection.upsert({ _id } as any, newRecord);
this.collection.update({ _id } as Mongo.Selector<T>, { $set: newRecord } as unknown as Mongo.Modifier<T>, { upsert: true });
}
await this.save();
}) as (...args: unknown[]) => void);
Expand Down Expand Up @@ -294,7 +294,7 @@ export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<
changes.push({
action: () => {
const { _id } = newRecord;
this.collection.upsert({ _id } as Mongo.Selector<T>, newRecord);
this.collection.update({ _id } as Mongo.Selector<T>, { $set: newRecord } as unknown as Mongo.Modifier<T>, { upsert: true });
if (actionTime > this.updatedAt) {
this.updatedAt = actionTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PrivateSettingsCachedCollection extends CachedCollection<ISetting> {
async setupListener(): Promise<void> {
sdk.stream('notify-logged', [this.eventName as 'private-settings-changed'], async (t: string, { _id, ...record }: { _id: string }) => {
this.log('record received', t, { _id, ...record });
this.collection.upsert({ _id }, record);
this.collection.update({ _id }, { $set: record }, { upsert: true });
this.sync();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Users } from './fixtures/userStates';
import { setSettingValueById } from './utils';
import { test, expect } from './utils/test';

test.use({ storageState: Users.admin.state });

test.describe.serial('settings-persistence-on-ui-navigation', () => {
test.beforeAll(({ api }) => setSettingValueById(api, 'Hide_System_Messages', []));

test.beforeEach(async ({ page }) => {
await page.goto('/admin/settings/Message');

// Intercept the API call and delay its response
await page.route('/api/v1/method.call/saveSettings', async (route) => {
const response = await route.fetch();
await new Promise((resolve) => setTimeout(resolve, 2000)); // Delay the response by 2 seconds
return route.fulfill({
response,
status: response.status(),
headers: response.headers(),
body: await response.body(),
});
});
});

test.afterAll(({ api }) => setSettingValueById(api, 'Hide_System_Messages', []));

test('expect settings to persist in ui when navigating back and forth', async ({ page }) => {
const settingInput = await page.locator('[data-qa-setting-id="Hide_System_Messages"] input');
await settingInput.pressSequentially('User joined');
await settingInput.press('Enter');

await page.locator('button:has-text("Save changes")').click();
await page.locator('button[title="Back"]').click();

await page.waitForResponse((response) => response.url().includes('/api/v1/method.call/saveSettings') && response.status() === 200);

await page.locator('a[href="/admin/settings/Message"] >> text=Open').click();

await expect(page.locator('label[for="Hide_System_Messages"][title="Hide_System_Messages"]')).toBeVisible();
});
});

0 comments on commit 96bb00f

Please sign in to comment.