forked from searchspring/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6add12a
commit 007ea24
Showing
8 changed files
with
477 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
packages/snap-platforms/shopify/src/pluginMutateResults.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import { pluginMutateResults } from './pluginMutateResults'; | ||
import { MockClient } from '@searchspring/snap-shared'; | ||
import { SearchStore } from '@searchspring/snap-store-mobx'; | ||
import { UrlManager, QueryStringTranslator, reactLinker } from '@searchspring/snap-url-manager'; | ||
import { EventManager } from '@searchspring/snap-event-manager'; | ||
import { Profiler } from '@searchspring/snap-profiler'; | ||
import { Logger } from '@searchspring/snap-logger'; | ||
import { Tracker } from '@searchspring/snap-tracker'; | ||
import { SearchController } from '@searchspring/snap-controller'; | ||
|
||
const ORIGIN = 'http://localhost'; | ||
const ROOT = 'root/'; | ||
|
||
const urlManager = new UrlManager(new QueryStringTranslator(), reactLinker); | ||
const services = { | ||
urlManager: urlManager, | ||
}; | ||
let searchConfig = { | ||
id: 'search', | ||
}; | ||
|
||
const globals = { siteId: '8uyt2m' }; | ||
const searchConfigDefault = { | ||
id: 'search', | ||
globals: { | ||
filters: [], | ||
}, | ||
settings: {}, | ||
}; | ||
let controller: any; | ||
let errMock: any; | ||
|
||
const controllerServices: any = { | ||
client: new MockClient(globals, {}), | ||
store: new SearchStore(searchConfig, services), | ||
urlManager, | ||
eventManager: new EventManager(), | ||
profiler: new Profiler(), | ||
logger: new Logger(), | ||
tracker: new Tracker(globals), | ||
}; | ||
|
||
describe('Shopify pluginMutateResults', () => { | ||
describe('requires shopify to exist on the dom', () => { | ||
beforeAll(async () => { | ||
searchConfig = { ...searchConfigDefault }; | ||
controller = new SearchController(searchConfig, controllerServices); | ||
errMock = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
}); | ||
|
||
beforeEach(() => { | ||
delete window.Shopify; | ||
}); | ||
|
||
afterEach(() => { | ||
errMock.mockRestore(); | ||
}); | ||
|
||
it('requires shopify to exist on the dom', () => { | ||
const config = { | ||
url: { | ||
enabled: true, | ||
}, | ||
}; | ||
|
||
pluginMutateResults(controller, config); | ||
|
||
expect(errMock).toHaveBeenCalledWith(expect.stringContaining('Error: window.Shopify not found'), expect.any(String), expect.any(String)); | ||
}); | ||
}); | ||
|
||
describe('has shopify in the dom', () => { | ||
beforeAll(async () => { | ||
errMock = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
}); | ||
|
||
beforeEach(() => { | ||
searchConfig = { ...searchConfigDefault }; | ||
controller = new SearchController(searchConfig, controllerServices); | ||
|
||
global.window = Object.create(window); | ||
|
||
Object.defineProperty(window, 'location', { | ||
value: { | ||
origin: ORIGIN, | ||
href: ORIGIN, | ||
}, | ||
writable: true, | ||
}); | ||
|
||
// @ts-ignore | ||
global.Shopify = { | ||
routes: { | ||
root: ROOT, | ||
}, | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
errMock.mockRestore(); | ||
}); | ||
|
||
it('requires config.enabled', async () => { | ||
const config = { | ||
url: { | ||
enabled: false, | ||
}, | ||
}; | ||
|
||
pluginMutateResults(controller, config); | ||
await controller.search(); | ||
|
||
expect(errMock).not.toHaveBeenCalled(); | ||
expect(controller.store.results[0].mappings.core?.url).toEqual(`/product/C-LTO-R6-L5316`); | ||
}); | ||
|
||
it('requires context.collection to be defined', async () => { | ||
const config = { | ||
url: { | ||
enabled: true, | ||
}, | ||
}; | ||
|
||
pluginMutateResults(controller, config); | ||
await controller.search(); | ||
|
||
expect(errMock).not.toHaveBeenCalled(); | ||
expect(controller.store.results[0].mappings.core?.url).toEqual(`/product/C-LTO-R6-L5316`); | ||
}); | ||
|
||
it('has context.collection defined', async () => { | ||
// plugin requires collection context | ||
const collectionContext = { | ||
handle: 'collection-handle', | ||
name: 'Collection Name', | ||
}; | ||
controller.context.collection = collectionContext; | ||
|
||
const config = { | ||
url: { | ||
enabled: true, | ||
}, | ||
}; | ||
|
||
pluginMutateResults(controller, config); | ||
|
||
await controller.search(); | ||
|
||
// plugin requires handle attribute | ||
expect(controller.store.results[0].attributes.handle).toBeDefined(); | ||
expect(errMock).not.toHaveBeenCalled(); | ||
|
||
expect(controller.store.results[0].mappings.core?.url).toEqual( | ||
`${ROOT}collections/${collectionContext.handle}/products/${controller.store.results[0].attributes.handle}` | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
packages/snap-preact/src/Templates/Stores/library/plugins/pluginScrollToTop.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import 'whatwg-fetch'; | ||
|
||
import { pluginScrollToTop, type ScrollBehavior } from './pluginScrollToTop'; | ||
import { MockClient } from '@searchspring/snap-shared'; | ||
import { SearchStore } from '@searchspring/snap-store-mobx'; | ||
import { UrlManager, QueryStringTranslator, reactLinker } from '@searchspring/snap-url-manager'; | ||
import { EventManager } from '@searchspring/snap-event-manager'; | ||
import { Profiler } from '@searchspring/snap-profiler'; | ||
import { Logger } from '@searchspring/snap-logger'; | ||
import { Tracker } from '@searchspring/snap-tracker'; | ||
import { SearchController } from '@searchspring/snap-controller'; | ||
|
||
const urlManager = new UrlManager(new QueryStringTranslator(), reactLinker); | ||
const services = { | ||
urlManager: urlManager, | ||
}; | ||
let searchConfig = { | ||
id: 'search', | ||
}; | ||
|
||
const globals = { siteId: '8uyt2m' }; | ||
const searchConfigDefault = { | ||
id: 'search', | ||
globals: { | ||
filters: [], | ||
}, | ||
settings: {}, | ||
}; | ||
let controller: any; | ||
let scrollMock: any; | ||
|
||
const wait = (time = 1) => { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, time); | ||
}); | ||
}; | ||
|
||
const controllerServices: any = { | ||
client: new MockClient(globals, {}), | ||
store: new SearchStore(searchConfig, services), | ||
urlManager, | ||
eventManager: new EventManager(), | ||
profiler: new Profiler(), | ||
logger: new Logger(), | ||
tracker: new Tracker(globals), | ||
}; | ||
|
||
describe('pluginScrollToTop', () => { | ||
beforeAll(async () => { | ||
scrollMock = jest.spyOn(global.window, 'scroll').mockImplementation(() => {}); | ||
}); | ||
|
||
beforeEach(() => { | ||
searchConfig = { ...searchConfigDefault }; | ||
controller = new SearchController(searchConfig, controllerServices); | ||
}); | ||
|
||
afterEach(() => { | ||
scrollMock.mockClear(); | ||
}); | ||
|
||
it('requires config.enabled', async () => { | ||
const config = { | ||
enabled: false, | ||
}; | ||
|
||
pluginScrollToTop(controller, config); | ||
await controller.search(); | ||
|
||
await wait(10); | ||
|
||
expect(scrollMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('can scroll with defaults', async () => { | ||
const config = { | ||
enabled: true, | ||
}; | ||
|
||
expect(controller.type).toEqual('search'); | ||
|
||
pluginScrollToTop(controller, config); | ||
await controller.search(); | ||
|
||
await wait(10); | ||
|
||
expect(scrollMock).toHaveBeenCalled(); | ||
expect(scrollMock).toHaveBeenCalledWith({ top: 0, left: 0, behavior: 'smooth' }); | ||
}); | ||
|
||
it('can scroll with options', async () => { | ||
const config = { | ||
enabled: true, | ||
options: { | ||
top: 100, | ||
left: 100, | ||
behavior: 'instant' as ScrollBehavior, | ||
}, | ||
}; | ||
|
||
pluginScrollToTop(controller, config); | ||
await controller.search(); | ||
|
||
await wait(10); | ||
|
||
expect(scrollMock).toHaveBeenCalled(); | ||
expect(scrollMock).toHaveBeenCalledWith(config.options); | ||
}); | ||
|
||
it('can scroll to selector', async () => { | ||
const config = { | ||
enabled: true, | ||
selector: '#test-selector', | ||
options: { | ||
top: 100, | ||
left: 100, | ||
behavior: 'instant' as ScrollBehavior, | ||
}, | ||
}; | ||
|
||
global.document.body.innerHTML = '<div style="position: relative;"><div id="test-selector" style="position: absolute; top: 100px;"></div></div>'; | ||
const element = document.querySelector('#test-selector'); | ||
jest.spyOn(element as any, 'getBoundingClientRect').mockImplementation(() => { | ||
// return new DOMRect(0, 0, 100, 500) //100px wide, 500px tall | ||
return { | ||
top: 100, | ||
}; | ||
}); | ||
expect(element?.getBoundingClientRect().top).toEqual(100); | ||
|
||
// @ts-ignore - override the element's getBoundingClientRect method | ||
element.getBoundingClientRect = () => { | ||
return { | ||
top: 100, | ||
}; | ||
}; | ||
|
||
pluginScrollToTop(controller, config); | ||
await controller.search(); | ||
|
||
await wait(10); | ||
|
||
expect(scrollMock).toHaveBeenCalled(); | ||
expect(scrollMock).toHaveBeenCalledWith({ top: 200, left: 100, behavior: 'instant' }); | ||
}); | ||
}); |
Oops, something went wrong.