|
| 1 | +import EmblaCarousel from '../components/EmblaCarousel' |
| 2 | +import { mockTestElements } from './mocks' |
| 3 | +import { FIXTURE_SLIDES_CHANGED } from './fixtures/slidesChanged.fixture' |
| 4 | +import { |
| 5 | + MockMutationRecordType, |
| 6 | + triggerMutationObserver |
| 7 | +} from './mocks/mutationObserver.mock' |
| 8 | + |
| 9 | +describe('➡️ SlidesChanged - MutationObserver', () => { |
| 10 | + describe('When DOM changes occur and the SLIDECHANGES option is set to TRUE', () => { |
| 11 | + const mutationRecord: MockMutationRecordType[] = [{ type: 'childList' }] |
| 12 | + |
| 13 | + test('The carousel WILL dispatch the slideschanged event and reinitialize', () => { |
| 14 | + const emblaApi = EmblaCarousel(mockTestElements(FIXTURE_SLIDES_CHANGED), { |
| 15 | + slideChanges: true |
| 16 | + }) |
| 17 | + |
| 18 | + const reInit = jest.spyOn(emblaApi, 'reInit') |
| 19 | + const callback = jest.fn() |
| 20 | + |
| 21 | + emblaApi.on('slideschanged', callback) |
| 22 | + triggerMutationObserver(mutationRecord) |
| 23 | + |
| 24 | + expect(callback).toHaveBeenCalledTimes(1) |
| 25 | + expect(reInit).toHaveBeenCalledTimes(1) |
| 26 | + }) |
| 27 | + |
| 28 | + test('The carousel will NOT dispatch the slideschanged event or reinitialize when destroyed', () => { |
| 29 | + const emblaApi = EmblaCarousel(mockTestElements(FIXTURE_SLIDES_CHANGED), { |
| 30 | + slideChanges: true |
| 31 | + }) |
| 32 | + |
| 33 | + const reInit = jest.spyOn(emblaApi, 'reInit') |
| 34 | + const callback = jest.fn() |
| 35 | + |
| 36 | + emblaApi.on('slideschanged', callback) |
| 37 | + emblaApi.destroy() |
| 38 | + |
| 39 | + triggerMutationObserver(mutationRecord) |
| 40 | + |
| 41 | + expect(callback).toHaveBeenCalledTimes(0) |
| 42 | + expect(reInit).toHaveBeenCalledTimes(0) |
| 43 | + }) |
| 44 | + |
| 45 | + test('A before callback that returns TRUE allows the internal default callback to run', () => { |
| 46 | + const emblaApi = EmblaCarousel(mockTestElements(FIXTURE_SLIDES_CHANGED), { |
| 47 | + slideChanges: true |
| 48 | + }) |
| 49 | + |
| 50 | + const reInit = jest.spyOn(emblaApi, 'reInit') |
| 51 | + |
| 52 | + emblaApi.on('slideschanged', () => true, { phase: 'before' }) |
| 53 | + triggerMutationObserver(mutationRecord) |
| 54 | + |
| 55 | + expect(reInit).toHaveBeenCalledTimes(1) |
| 56 | + }) |
| 57 | + |
| 58 | + test('A before callback that returns FALSE blocks the internal default callback', () => { |
| 59 | + const emblaApi = EmblaCarousel(mockTestElements(FIXTURE_SLIDES_CHANGED), { |
| 60 | + slideChanges: true |
| 61 | + }) |
| 62 | + |
| 63 | + const reInit = jest.spyOn(emblaApi, 'reInit') |
| 64 | + |
| 65 | + emblaApi.on('slideschanged', () => false, { phase: 'before' }) |
| 66 | + triggerMutationObserver(mutationRecord) |
| 67 | + |
| 68 | + expect(reInit).toHaveBeenCalledTimes(0) |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + describe('When DOM changes occur and the SLIDECHANGES option is set to FALSE', () => { |
| 73 | + const mutationRecord: MockMutationRecordType[] = [{ type: 'childList' }] |
| 74 | + |
| 75 | + test('The slideschanged event is NOT dispatched and the carousel does NOT reinitialize', () => { |
| 76 | + const emblaApi = EmblaCarousel(mockTestElements(FIXTURE_SLIDES_CHANGED), { |
| 77 | + slideChanges: false |
| 78 | + }) |
| 79 | + |
| 80 | + const reInit = jest.spyOn(emblaApi, 'reInit') |
| 81 | + const callback = jest.fn() |
| 82 | + |
| 83 | + emblaApi.on('slideschanged', callback) |
| 84 | + triggerMutationObserver(mutationRecord) |
| 85 | + |
| 86 | + expect(callback).toHaveBeenCalledTimes(0) |
| 87 | + expect(reInit).toHaveBeenCalledTimes(0) |
| 88 | + }) |
| 89 | + |
| 90 | + test('A before callback does NOT run at all', () => { |
| 91 | + const emblaApi = EmblaCarousel(mockTestElements(FIXTURE_SLIDES_CHANGED), { |
| 92 | + slideChanges: false |
| 93 | + }) |
| 94 | + |
| 95 | + const callback = jest.fn(() => true) |
| 96 | + |
| 97 | + emblaApi.on('slideschanged', callback, { phase: 'before' }) |
| 98 | + triggerMutationObserver(mutationRecord) |
| 99 | + |
| 100 | + expect(callback).toHaveBeenCalledTimes(0) |
| 101 | + }) |
| 102 | + }) |
| 103 | +}) |
0 commit comments