-
Notifications
You must be signed in to change notification settings - Fork 2
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
d88ba5d
commit 2074fbb
Showing
3 changed files
with
67 additions
and
40 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
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 |
---|---|---|
@@ -1,42 +1,43 @@ | ||
const VideoModal = require('../scripts/videopage'); | ||
|
||
// Mocking fetch globally | ||
global.fetch = jest.fn(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve({ likes: 10, liked: true }) | ||
}) | ||
); | ||
|
||
describe('VideoModal - openModal', () => { | ||
let mockModal, mockVideoFrame; | ||
|
||
beforeEach(() => { | ||
// Reset mocks before each test | ||
fetch.mockClear(); | ||
|
||
// Mock modal and videoFrame elements | ||
document.getElementById = jest.fn((id) => { | ||
if (id === 'modal') { | ||
return { style: { display: 'none' } }; // Mock modal element with style | ||
} | ||
if (id === 'videoFrame') { | ||
return { src: '' }; // Mock videoFrame element with src | ||
} | ||
return null; | ||
}); | ||
mockModal = { style: { display: 'none' } }; | ||
mockVideoFrame = { src: '' }; | ||
|
||
document.getElementsByClassName = jest.fn(className => { | ||
if (className === 'close') return [{ onclick: null }]; | ||
return []; | ||
document.getElementById = jest.fn().mockImplementation((id) => { | ||
if (id === 'modal') return mockModal; | ||
if (id === 'videoFrame') return mockVideoFrame; | ||
return null; | ||
}); | ||
|
||
document.querySelectorAll = jest.fn(selector => { | ||
if (selector === '.video-card') { | ||
return [{ | ||
addEventListener: jest.fn(), | ||
getAttribute: jest.fn(name => { | ||
if (name === 'data-video-url') return 'https://www.example.com/watch?v=example'; | ||
}) | ||
}]; | ||
} | ||
return []; | ||
}); | ||
document.getElementsByClassName = jest.fn().mockReturnValue([{ onclick: null }]); | ||
document.querySelectorAll = jest.fn().mockReturnValue([{ | ||
addEventListener: jest.fn(), | ||
getAttribute: jest.fn().mockReturnValue('https://www.example.com/watch?v=example') | ||
}]); | ||
}); | ||
|
||
it('sets videoFrame src and displays modal', () => { | ||
const videoModal = new VideoModal('modal', 'videoFrame', 'close', '.video-card'); | ||
const testUrl = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'; | ||
videoModal.openModal(testUrl); | ||
|
||
expect(videoModal.videoFrame.src).toContain('https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=1&rel=0'); | ||
expect(videoModal.modal.style.display).toBe('block'); | ||
expect(mockVideoFrame.src).toContain('https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=1&rel=0'); | ||
expect(mockModal.style.display).toBe('block'); | ||
}); | ||
}); |
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