Skip to content

Commit

Permalink
Better organized backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
latonv committed Sep 22, 2022
1 parent d41c50d commit e6e1e83
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 150 deletions.
129 changes: 54 additions & 75 deletions test/search-backend/fulltext-search-backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,91 +9,70 @@ import {
} from '../../src/search-service-error';

describe('FulltextSearchBackend', () => {
it('can perform a search', async () => {
const fetchBackup = window.fetch;
window.fetch = (): Promise<Response> => {
return new Promise(resolve => {
describe('basic fetch behavior', () => {
let fetchBackup: typeof window.fetch;
let urlCalled: RequestInfo | URL;
let urlConfig: RequestInit | undefined;

beforeEach(() => {
fetchBackup = window.fetch;
urlCalled = '';
urlConfig = undefined;

window.fetch = (
url: RequestInfo | URL,
config?: RequestInit
): Promise<Response> => {
urlCalled = url;
urlConfig = config;
const response = new Response('{ "foo": "bar" }');
resolve(response);
});
};
return new Promise(resolve => resolve(response));
};
});

const backend = new FulltextSearchBackend();
const params = { query: 'foo' };
const result = await backend.performSearch(params);
expect(result.success?.foo).to.equal('bar');
window.fetch = fetchBackup;
});
afterEach(() => {
window.fetch = fetchBackup;
});

it('sets the fts service backend', async () => {
const fetchBackup = window.fetch;
let urlCalled: RequestInfo | URL;
let urlConfig: RequestInit | undefined;
window.fetch = (
url: RequestInfo | URL,
config?: RequestInit
): Promise<Response> => {
urlCalled = url;
urlConfig = config;
const response = new Response('boop');
return new Promise(resolve => resolve(response));
};
it('can perform a search', async () => {
const backend = new FulltextSearchBackend();
const params = { query: 'foo' };
const result = await backend.performSearch(params);

const backend = new FulltextSearchBackend();
await backend.performSearch({ query: 'foo' });
expect(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
new URL(urlCalled!.toString()).searchParams.get('service_backend')
).to.equal('fts');
window.fetch = fetchBackup;
});
expect(result.success?.foo).to.equal('bar');
});

it('uses the provided service path', async () => {
const fetchBackup = window.fetch;
let urlCalled: RequestInfo | URL;
let urlConfig: RequestInit | undefined;
window.fetch = (
url: RequestInfo | URL,
config?: RequestInit
): Promise<Response> => {
urlCalled = url;
urlConfig = config;
const response = new Response('boop');
return new Promise(resolve => resolve(response));
};
it('sets the fts service backend', async () => {
const backend = new FulltextSearchBackend();
await backend.performSearch({ query: 'foo' });

const backend = new FulltextSearchBackend({
baseUrl: 'foo.bar',
servicePath: '/baz',
expect(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
new URL(urlCalled!.toString()).searchParams.get('service_backend')
).to.equal('fts');
});
await backend.performSearch({ query: 'boop' });
expect(urlCalled!.toString()).to.satisfy((url: string) =>
url.startsWith('https://foo.bar/baz')
);
window.fetch = fetchBackup;
});

it('includes credentials for search endpoint if requested', async () => {
const fetchBackup = window.fetch;
let urlCalled: RequestInfo | URL;
let urlConfig: RequestInit | undefined;
window.fetch = (
url: RequestInfo | URL,
config?: RequestInit
): Promise<Response> => {
urlCalled = url;
urlConfig = config;
const response = new Response('boop');
return new Promise(resolve => resolve(response));
};
it('uses the provided service path', async () => {
const backend = new FulltextSearchBackend({
baseUrl: 'foo.bar',
servicePath: '/baz',
});
await backend.performSearch({ query: 'boop' });

const backend = new FulltextSearchBackend({
scope: 'foo',
includeCredentials: true,
expect(urlCalled!.toString()).to.satisfy((url: string) =>
url.startsWith('https://foo.bar/baz')
);
});

it('includes credentials for search endpoint if requested', async () => {
const backend = new FulltextSearchBackend({
scope: 'foo',
includeCredentials: true,
});
await backend.performSearch({ query: 'foo' });

expect(urlConfig?.credentials).to.equal('include');
});
await backend.performSearch({ query: 'foo' });
expect(urlConfig?.credentials).to.equal('include');
window.fetch = fetchBackup;
});

it('returns a network error result upon fetch errors', async () => {
Expand Down
129 changes: 54 additions & 75 deletions test/search-backend/metadata-search-backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,91 +9,70 @@ import {
} from '../../src/search-service-error';

describe('MetadataSearchBackend', () => {
it('can perform a search', async () => {
const fetchBackup = window.fetch;
window.fetch = (): Promise<Response> => {
return new Promise(resolve => {
describe('basic fetch behavior', () => {
let fetchBackup: typeof window.fetch;
let urlCalled: RequestInfo | URL;
let urlConfig: RequestInit | undefined;

beforeEach(() => {
fetchBackup = window.fetch;
urlCalled = '';
urlConfig = undefined;

window.fetch = (
url: RequestInfo | URL,
config?: RequestInit
): Promise<Response> => {
urlCalled = url;
urlConfig = config;
const response = new Response('{ "foo": "bar" }');
resolve(response);
});
};
return new Promise(resolve => resolve(response));
};
});

const backend = new MetadataSearchBackend();
const params = { query: 'foo' };
const result = await backend.performSearch(params);
expect(result.success?.foo).to.equal('bar');
window.fetch = fetchBackup;
});
afterEach(() => {
window.fetch = fetchBackup;
});

it('sets the metadata service backend', async () => {
const fetchBackup = window.fetch;
let urlCalled: RequestInfo | URL;
let urlConfig: RequestInit | undefined;
window.fetch = (
url: RequestInfo | URL,
config?: RequestInit
): Promise<Response> => {
urlCalled = url;
urlConfig = config;
const response = new Response('boop');
return new Promise(resolve => resolve(response));
};
it('can perform a search', async () => {
const backend = new MetadataSearchBackend();
const params = { query: 'foo' };
const result = await backend.performSearch(params);

const backend = new MetadataSearchBackend();
await backend.performSearch({ query: 'foo' });
expect(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
new URL(urlCalled!.toString()).searchParams.get('service_backend')
).to.equal('metadata');
window.fetch = fetchBackup;
});
expect(result.success?.foo).to.equal('bar');
});

it('uses the provided service path', async () => {
const fetchBackup = window.fetch;
let urlCalled: RequestInfo | URL;
let urlConfig: RequestInit | undefined;
window.fetch = (
url: RequestInfo | URL,
config?: RequestInit
): Promise<Response> => {
urlCalled = url;
urlConfig = config;
const response = new Response('boop');
return new Promise(resolve => resolve(response));
};
it('sets the metadata service backend', async () => {
const backend = new MetadataSearchBackend();
await backend.performSearch({ query: 'foo' });

const backend = new MetadataSearchBackend({
baseUrl: 'foo.bar',
servicePath: '/baz',
expect(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
new URL(urlCalled!.toString()).searchParams.get('service_backend')
).to.equal('metadata');
});
await backend.performSearch({ query: 'boop' });
expect(urlCalled!.toString()).to.satisfy((url: string) =>
url.startsWith('https://foo.bar/baz')
);
window.fetch = fetchBackup;
});

it('includes credentials for search endpoint if requested', async () => {
const fetchBackup = window.fetch;
let urlCalled: RequestInfo | URL;
let urlConfig: RequestInit | undefined;
window.fetch = (
url: RequestInfo | URL,
config?: RequestInit
): Promise<Response> => {
urlCalled = url;
urlConfig = config;
const response = new Response('boop');
return new Promise(resolve => resolve(response));
};
it('uses the provided service path', async () => {
const backend = new MetadataSearchBackend({
baseUrl: 'foo.bar',
servicePath: '/baz',
});
await backend.performSearch({ query: 'boop' });

const backend = new MetadataSearchBackend({
scope: 'foo',
includeCredentials: true,
expect(urlCalled!.toString()).to.satisfy((url: string) =>
url.startsWith('https://foo.bar/baz')
);
});

it('includes credentials for search endpoint if requested', async () => {
const backend = new MetadataSearchBackend({
scope: 'foo',
includeCredentials: true,
});
await backend.performSearch({ query: 'foo' });

expect(urlConfig?.credentials).to.equal('include');
});
await backend.performSearch({ query: 'foo' });
expect(urlConfig?.credentials).to.equal('include');
window.fetch = fetchBackup;
});

it('returns a network error result upon fetch errors', async () => {
Expand Down

0 comments on commit e6e1e83

Please sign in to comment.