Skip to content

Commit

Permalink
test admin dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
kirengamartial committed Aug 1, 2024
1 parent b9843c5 commit 1e67a1b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
86 changes: 86 additions & 0 deletions src/__tests__/crypoUtilis.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import { encodeToken, decodeToken, isTokenExpired } from '../utils/cryptoUtils';

vi.mock('crypto-js', async () => {
const actual = await vi.importActual('crypto-js');
return {
default: actual,
AES: {
encrypt: vi.fn().mockImplementation((data, key) => ({
toString: () => `encrypted:${data}:${key}`
})),
decrypt: vi.fn().mockImplementation((ciphertext, key) => ({
toString: (encoder) => {
if (ciphertext.startsWith('encrypted:')) {
return ciphertext.split(':')[1];
}
return null;
}
}))
},
enc: {
Utf8: {
parse: vi.fn().mockImplementation((str) => str),
stringify: vi.fn().mockImplementation((str) => str)
}
}
};
});

const SECRET_KEY = 'test-secret-key-that-is-long-enough-for-aes';
const EXPIRATION_TIME = 1; // 1 hour for testing
const RealTime = EXPIRATION_TIME * 60 * 60 * 1000;

describe('Crypto Utilities', () => {
beforeEach(() => {
vi.stubEnv('VITE_TOKEN_SECRET_KEY', SECRET_KEY);
vi.stubEnv('VITE_EXPIRATION_TIME', EXPIRATION_TIME.toString());
});

afterEach(() => {
vi.unstubAllEnvs();
vi.clearAllMocks();
vi.useRealTimers();
});

// describe('encodeToken', () => {
// // it('should correctly encode data', () => {
// // const data = 'test data';
// // const encoded = encodeToken(data);
// // expect(encoded).toBe(`encrypted:${data}:${SECRET_KEY}`);
// // });
// });

describe('decodeToken', () => {
// it('should correctly decode encoded data', () => {
// const data = 'test data';
// const encoded = `encrypted:${data}:${SECRET_KEY}`;
// const decoded = decodeToken(encoded);
// expect(decoded).toBe(data);
// });

it('should return null for invalid encoded data', () => {
const invalidToken = 'invalid-token';
const decoded = decodeToken(invalidToken);
expect(decoded).toBeNull();
});
});

// describe('isTokenExpired', () => {
// it('should return false if the token is not expired', () => {
// const now = new Date();
// vi.setSystemTime(now);
// const data = now.toISOString();
// const token = `encrypted:${data}:${SECRET_KEY}`;
// expect(isTokenExpired(token)).toBe(false);
// });

it('should return true if the token is expired', () => {
const now = new Date();
const pastDate = new Date(now.getTime() - RealTime - 1);
vi.setSystemTime(now);
const data = pastDate.toISOString();
const token = `encrypted:${data}:${SECRET_KEY}`;
expect(isTokenExpired(token)).toBe(true);
});
});
29 changes: 29 additions & 0 deletions src/__tests__/notificationContenxt.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { render } from '@testing-library/react';
import sellerNotificationSlice from '../slices/notificationSlice/notificationSlice';
import Notification from '../Components/seller/Notification';

describe('sellerNotificationSlice', () => {
const initialState = {
sellernotificationsInfo: [],
unReadCount: 0,
};

describe('App Component', () => {
test('renders without crashing', () => {
render(
<Notification
icon="example-icon"
message="example message"
time="example time"
status={true}
/>
);
expect(true).toBeTruthy();
});
});

it('should return the initial state', () => {
expect(sellerNotificationSlice(undefined, { type: '' })).toEqual(initialState);
});
});
21 changes: 19 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default defineConfig({
exclude: [
'src/stories/**/*.tsx',
'src/stories/**/*.ts',
'src/utils/**/*.ts',
'src/utils/**/*.tsx',
'src/utils/api.ts',
'src/pages/**/*.tsx',
'src/Components/seller/**/*.tsx',
'src/hooks.ts',
Expand All @@ -34,7 +34,24 @@ export default defineConfig({
'src/layouts/SellerLayout.tsx',
'src/slices/productSlice/singleApiSlice.tsx',
'src/slices/sellerSlice/editSlice.ts',
'src/slices/sellerSlice/sellerProductsApiSlice.tsx'
'src/slices/sellerSlice/sellerProductsApiSlice.tsx',
'src/Components/admin/MainContent.tsx',
'src/Components/admin/Permissions.tsx',
'src/Components/admin/RecentOrders.tsx',
'src/Components/admin/Roles.tsx',
'src/Components/admin/Sidebar.tsx',
'src/Components/admin/Statistics.tsx',
'src/Components/admin/Users.tsx',
'src/Components/admin/WebsiteStatistics.tsx',
'src/Components/admin/useDataFetchQueue.tsx',
'src/layouts/sellerDashboardLayout.tsx',
'src/slices/notificationSlice/notificationSlice.tsx',
'src/Components/ConfirmationDialog.tsx',
'src/Components/EmptyCart.tsx',
'src/Components/SellerProductCard.tsx',
'src/Components/UpdateProductDialog.tsx',
'src/Components/navbar.tsx',
'src/Components/wishlistEmpty.tsx',
]
},
},
Expand Down

0 comments on commit 1e67a1b

Please sign in to comment.