I've a laravel vue setup, how can I mock document.head.querySelector with jest. I've to import a file in jest-setup.js in order to mock other functions and libraries but it also have this code which needs a spy function on document.head.querySelector:
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
axios.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found');
}
I want to mock document.head.querySelector to something like
jest.spyOn(document, 'querySelector').mockImplementation((selector) => {
switch (selector) {
case '.meta[name="csrf-token"]':
return 'test-token';
}
});
but this doesn't works as I'm not mocking the head.querySelector which I don't know how to.