|
| 1 | +import ClientCookies from 'js-cookie' |
| 2 | + |
| 3 | +import { getCookie, removeCookie, setCookie } from '..' |
| 4 | + |
| 5 | +jest.mock('js-cookie', () => ({ |
| 6 | + get: jest.fn(), |
| 7 | + set: jest.fn(), |
| 8 | + remove: jest.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +describe('Cookie Functions in the client side', () => { |
| 12 | + describe('getCookie', () => { |
| 13 | + it('should get a cookie from the client', () => { |
| 14 | + const mockCookie = 'test-cookie' |
| 15 | + ;(ClientCookies.get as jest.Mock).mockReturnValue(mockCookie) |
| 16 | + |
| 17 | + const result = getCookie('test') |
| 18 | + expect(result).toBe(mockCookie) |
| 19 | + expect(ClientCookies.get).toHaveBeenCalledWith('test') |
| 20 | + }) |
| 21 | + |
| 22 | + it('should be able to parse the cookie if it is a JSON string', () => { |
| 23 | + const mockCookie = '{"key": "value"}' |
| 24 | + ;(ClientCookies.get as jest.Mock).mockReturnValue(mockCookie) |
| 25 | + |
| 26 | + const result = getCookie('test') |
| 27 | + |
| 28 | + expect(result).toEqual({ key: 'value' }) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should return raw cookie if JSON parsing fails', () => { |
| 32 | + ;(ClientCookies.get as jest.Mock).mockImplementation(() => '{"malformedJson":') |
| 33 | + |
| 34 | + const result = getCookie('test') |
| 35 | + |
| 36 | + expect(result).toBe('{"malformedJson":') |
| 37 | + }) |
| 38 | + |
| 39 | + it('should not parse the cookie if parseJSON is false', () => { |
| 40 | + const mockCookie = '{"key": "value"}' |
| 41 | + ;(ClientCookies.get as jest.Mock).mockReturnValue(mockCookie) |
| 42 | + |
| 43 | + const result = getCookie('test', { parseJSON: false }) |
| 44 | + |
| 45 | + expect(result).toBe(mockCookie) |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + describe('setCookie', () => { |
| 50 | + it('should set a cookie', () => { |
| 51 | + const key = 'test' |
| 52 | + const value = { data: 'test-data' } |
| 53 | + const config = { expires: 7 } |
| 54 | + |
| 55 | + setCookie(key, value, config) |
| 56 | + expect(ClientCookies.set).toHaveBeenCalledWith(key, JSON.stringify(value), config) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should handle errors gracefully', () => { |
| 60 | + ;(ClientCookies.set as jest.Mock).mockImplementation(() => { |
| 61 | + throw new Error('error') |
| 62 | + }) |
| 63 | + |
| 64 | + expect(() => setCookie('test', 'value', {})).not.toThrow() |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + describe('removeCookie', () => { |
| 69 | + it('should remove a cookie', () => { |
| 70 | + const key = 'test' |
| 71 | + |
| 72 | + removeCookie(key) |
| 73 | + expect(ClientCookies.remove).toHaveBeenCalledWith(key) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should handle errors gracefully', () => { |
| 77 | + ;(ClientCookies.remove as jest.Mock).mockImplementation(() => { |
| 78 | + throw new Error('error') |
| 79 | + }) |
| 80 | + |
| 81 | + expect(() => removeCookie('test')).not.toThrow() |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
0 commit comments