Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): pass scoped API to lifecycle hooks #422

Merged
merged 1 commit into from
Feb 1, 2021
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
78 changes: 42 additions & 36 deletions packages/autocomplete-core/src/__tests__/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,33 @@ describe('completion', () => {
inputElement.focus();
userEvent.type(inputElement, '{arrowdown}');

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
completion: '1',
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
completion: '1',
}),
})
);

userEvent.type(inputElement, '{arrowdown}');

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
completion: '2',
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
completion: '2',
}),
})
);

userEvent.type(inputElement, '{arrowdown}');

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
completion: null,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
completion: null,
}),
})
);
});

test('does not set completion when panel is closed', () => {
Expand All @@ -74,12 +77,13 @@ describe('completion', () => {
inputElement.focus();
userEvent.type(inputElement, '{esc}{arrowdown}');

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
completion: null,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
completion: null,
}),
})
);
});

test('does not set completion when no activeItemId', () => {
Expand All @@ -92,12 +96,13 @@ describe('completion', () => {
inputElement.focus();
userEvent.type(inputElement, '{arrowdown}');

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
completion: null,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
completion: null,
}),
})
);
});

test('does not set completion without itemInputValue', () => {
Expand All @@ -117,11 +122,12 @@ describe('completion', () => {
inputElement.focus();
userEvent.type(inputElement, '{arrowdown}');

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
completion: null,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
completion: null,
}),
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ describe('createAutocomplete', () => {

test('subscribes all plugins', () => {
const plugin = { subscribe: jest.fn() };
createAutocomplete({ plugins: [plugin] });
const autocomplete = createAutocomplete({ plugins: [plugin] });

expect(plugin.subscribe).toHaveBeenCalledTimes(1);
expect(plugin.subscribe).toHaveBeenLastCalledWith({
onActive: expect.any(Function),
onSelect: expect.any(Function),
setCollections: expect.any(Function),
setContext: expect.any(Function),
setIsOpen: expect.any(Function),
setQuery: expect.any(Function),
setActiveItemId: expect.any(Function),
setStatus: expect.any(Function),
refresh: autocomplete.refresh,
setCollections: autocomplete.setCollections,
setContext: autocomplete.setContext,
setIsOpen: autocomplete.setIsOpen,
setQuery: autocomplete.setQuery,
setActiveItemId: autocomplete.setActiveItemId,
setStatus: autocomplete.setStatus,
});
});
});
9 changes: 5 additions & 4 deletions packages/autocomplete-core/src/__tests__/debug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ describe('debug', () => {
inputElement.focus();
inputElement.blur();

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({ isOpen: true }),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({ isOpen: true }),
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ describe('defaultActiveItemId', () => {

userEvent.type(inputElement, 'a');

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
activeItemId: null,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
activeItemId: null,
}),
})
);
});

test('selects provided defaultActiveItemId on open (onInput)', () => {
Expand All @@ -39,12 +40,13 @@ describe('defaultActiveItemId', () => {

inputElement.focus();

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
activeItemId: 0,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
activeItemId: 0,
}),
})
);
});

test('selects defaultActiveItemId with openOnFocus on reset', () => {
Expand All @@ -66,12 +68,13 @@ describe('defaultActiveItemId', () => {
autocomplete.setActiveItemId(null);
formElement.reset();

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
activeItemId: 0,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
activeItemId: 0,
}),
})
);
});

test('selects defaultActiveItemId on focus', () => {
Expand All @@ -88,12 +91,13 @@ describe('defaultActiveItemId', () => {

inputElement.focus();

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
activeItemId: 0,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
activeItemId: 0,
}),
})
);
});

test('selects defaultActiveItemId when ArrowDown on the last', () => {
Expand All @@ -114,12 +118,13 @@ describe('defaultActiveItemId', () => {

userEvent.type(inputElement, '{arrowdown}');

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
activeItemId: 0,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
activeItemId: 0,
}),
})
);
});

test('selects defaultActiveItemId when ArrowUp on the first', () => {
Expand All @@ -140,11 +145,12 @@ describe('defaultActiveItemId', () => {

userEvent.type(inputElement, '{arrowup}');

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
activeItemId: 0,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
activeItemId: 0,
}),
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ describe('getEnvironmentProps', () => {
});
window.document.dispatchEvent(customEvent);

expect(onStateChange).toHaveBeenLastCalledWith({
prevState: expect.anything(),
state: expect.objectContaining({
activeItemId: null,
isOpen: false,
}),
});
expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
activeItemId: null,
isOpen: false,
}),
})
);
});
});

Expand Down
Loading