Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { fireEvent, screen, waitFor } from '@testing-library/vue';
import { flushPromises } from '@vue/test-utils';
import { HttpResponse, http } from 'msw';
import { expect, it, vi } from 'vitest';

import Sessions from './index.vue';

import { applications } from '@/mocks/applications/data';
import { server } from '@/mocks/server';
import Application from '@/services/application';
import { render } from '@/test-utils';

const showSessions = (query: Record<string, string>) => {
const instance = new Application(applications[0]).instances[0];
render(Sessions, {
props: { instance },
global: {
mocks: {
$route: { name: 'instances/sessions', query },
$router: { replace: vi.fn() },
},
},
});
return instance;
};

it("keeps the current user's sessions when an earlier lookup completes last", async () => {
const release = Promise.withResolvers<void>();
server.use(
http.get('*/actuator/sessions', async ({ request }) => {
const username = new URL(request.url).searchParams.get('username');
if (username === 'alice') await release.promise;
return HttpResponse.json({ sessions: [{ id: username + '-session' }] });
}),
);
const instance = showSessions({ username: 'alice' });
const fetch = vi.spyOn(instance, 'fetchSessionsByUsername');
await waitFor(() => expect(fetch).toHaveBeenCalledWith('alice'));
await fireEvent.update(screen.getByDisplayValue('alice'), 'bob');
expect(await screen.findByText('bob-session')).toBeVisible();
release.resolve();
await fetch.mock.results[0].value;
await flushPromises();
expect(screen.queryByText('alice-session')).not.toBeInTheDocument();
expect(screen.getByText('bob-session')).toBeVisible();
});

it('submits an ID search immediately without querying for a username', async () => {
server.use(
http.get('*/actuator/sessions/:id', ({ params }) =>
HttpResponse.json({ id: params.id }),
),
);
const instance = showSessions({ sessionId: 'first-id' });
await screen.findByText('first-id');
const byUsername = vi.spyOn(instance, 'fetchSessionsByUsername');
const byId = vi.spyOn(instance, 'fetchSession');
const input = screen.getByDisplayValue('first-id');
await fireEvent.update(input, 'second-id');
await fireEvent.keyUp(input, { key: 'Enter' });
expect(byId).toHaveBeenCalledWith('second-id');
expect(byUsername).not.toHaveBeenCalled();
expect(await screen.findByText('second-id')).toBeVisible();
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
name="filter"
type="search"
@paste="handlePaste"
@keyup.enter="fetchSessionsByUsername()"
@keyup.enter="fetch()"
/>
</div>
</sba-sticky-subnav>
Expand Down Expand Up @@ -94,19 +94,24 @@ export default {
sessions: [],
isLoading: false,
currentRouteName: null,
requestId: 0,
scheduleFetch: debounce(function () {
this.fetch();
}, 250),
}),
watch: {
'$route.query': {
immediate: true,
handler() {
this.filter = Object.entries(this.$route.query).reduce(
const filter = Object.entries(this.$route.query).reduce(
(acc, [name, value]) => {
acc.type = name;
acc.value = value;
return acc;
},
{ type: 'username', value: '' },
);
if (!isEqual(filter, this.filter)) this.filter = filter;
},
},
filter: {
Expand All @@ -123,40 +128,54 @@ export default {
query: oldQuery,
});
}
this.fetch();
this.requestId++;
this.sessions = [];
this.error = null;
this.isLoading = Boolean(this.filter.value);
this.scheduleFetch();
},
},
},
mounted() {
this.currentRouteName = this.$route.name;
},
beforeUnmount() {
this.scheduleFetch.cancel();
this.requestId++;
},
methods: {
fetch: debounce(async function () {
async fetch() {
this.scheduleFetch.cancel();
const requestId = ++this.requestId;
this.error = null;
if (!this.filter.value) {
this.sessions = [];
this.isLoading = false;
return;
}

this.isLoading = true;
try {
if (this.filter.type === 'sessionId') {
this.sessions = await this.fetchSession();
} else {
this.sessions = await this.fetchSessionsByUsername();
}
const sessions =
this.filter.type === 'sessionId'
? await this.fetchSession()
: await this.fetchSessionsByUsername();
if (requestId === this.requestId) this.sessions = sessions;
} catch (error) {
console.warn('Fetching sessions failed:', error);
this.error = error;
if (requestId === this.requestId) {
console.warn('Fetching sessions failed:', error);
this.error = error;
}
} finally {
if (requestId === this.requestId) this.isLoading = false;
}
this.isLoading = false;
}, 250),
},
async fetchSession() {
try {
const response = await this.instance.fetchSession(this.filter.value);
return [new Session(response.data)];
} catch (error) {
if (error.response.status === 404) {
if (error.response?.status === 404) {
return [];
} else {
throw error;
Expand Down
Loading