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
18 changes: 11 additions & 7 deletions src/capacitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ export class FluxCapacitor extends EventEmitter {
this.emit(Events.SEARCH, this.query.raw);
return this.bridge.search(this.query)
.then((results) => {
if (results.redirect) this.emit(Events.REDIRECT, results.redirect);
this.emitQueryChanged(originalQuery);

// must be in this order
const oldQuery = this.originalQuery;
Object.assign(this, { results, originalQuery });

if (results.redirect) {
this.emit(Events.REDIRECT, results.redirect);
}
this.emit(Events.RESULTS, results);
this.emitQueryChanged(oldQuery, originalQuery);

return results;
});
Expand All @@ -77,7 +79,7 @@ export class FluxCapacitor extends EventEmitter {
rewrite(query: string, config: RewriteConfig = {}): Promise<string> {
let search;
if (config.skipSearch) {
this.emitQueryChanged(query);
this.emitQueryChanged(this.originalQuery, query);
search = Promise.resolve(this.query.withQuery(this.originalQuery = query));
} else {
search = this.search(query);
Expand Down Expand Up @@ -138,8 +140,10 @@ export class FluxCapacitor extends EventEmitter {
return this.search();
}

private emitQueryChanged(query: string) {
if (query !== this.originalQuery) this.emit(Events.QUERY_CHANGED);
private emitQueryChanged(oldQuery: string, newQuery: string) {
if (oldQuery.toLowerCase() !== newQuery.toLowerCase()) {
this.emit(Events.QUERY_CHANGED);
}
}

private get filteredRequest() {
Expand Down
26 changes: 18 additions & 8 deletions test/capacitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ describe('FluxCapacitor', function() {
});

it('should emit a results event', (done) => {
mock.post(SEARCH_URL, (req, res) => res.body('ok'));
flux.bridge.search = (): any => Promise.resolve('ok');
// mock.post(SEARCH_URL, (req, res) => res.body('ok'));
flux.on(Events.RESULTS, () => done());
flux.search('');
});
Expand All @@ -105,21 +106,30 @@ describe('FluxCapacitor', function() {
});

it('should not emit a query_changed event on subsequent equivalent requests', (done) => {
mock.post(SEARCH_URL, (req, res) => res.body('ok'));
flux.on(Events.QUERY_CHANGED, () => expect.fail());
flux.bridge.search = (): any => Promise.resolve('ok');

flux.search('')
.then(() => flux.search(''))
flux.search('apple')
.then(() => flux.on(Events.QUERY_CHANGED, () => expect.fail()))
.then(() => flux.search('apple'))
.then(() => done());
});

it('should emit a query_changed event on changing the query', (done) => {
mock.post(SEARCH_URL, (req, res) => res.body('ok'));
flux.on(Events.QUERY_CHANGED, () => done());
flux.bridge.search = (): any => Promise.resolve('ok');

flux.search('')
flux.search('shoes')
.then(() => flux.on(Events.QUERY_CHANGED, () => done()))
.then(() => flux.search('other'));
});

it('should emit a query_changed with case insensitivity', (done) => {
flux.bridge.search = (): any => Promise.resolve('ok');

flux.search('apple')
.then(() => flux.on(Events.QUERY_CHANGED, () => expect.fail()))
.then(() => flux.search('ApPle'))
.then(() => done());
});
});
});

Expand Down