Skip to content

Commit

Permalink
Create userTest.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 28, 2024
1 parent 681b87f commit 65a5814
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { test, expect } from '@jest/globals';
import { User } from '../models/userModel';
import { generateToken, verifyToken } from '../utils/auth';
import { userReducer } from '../reducers/userReducer';

describe('User Tests', () => {
test('Create a new user', async () => {
const user = new User({
name: 'John Doe',
email: 'john.doe@example.com',
password: 'password123',
});
expect(user).toHaveProperty('id');
expect(user.name).toBe('John Doe');
expect(user.email).toBe('john.doe@example.com');
});

test('Generate a token for a user', async () => {
const user = new User({
name: 'John Doe',
email: 'john.doe@example.com',
password: 'password123',
});
const token = generateToken(user);
expect(token).toBeInstanceOf(String);
});

test('Verify a token for a user', async () => {
const user = new User({
name: 'John Doe',
email: 'john.doe@example.com',
password: 'password123',
});
const token = generateToken(user);
const verifiedUser = verifyToken(token);
expect(verifiedUser).toHaveProperty('id');
expect(verifiedUser.name).toBe('John Doe');
expect(verifiedUser.email).toBe('john.doe@example.com');
});

test('User reducer should handle SET_USER action', async () => {
const initialState = {};
const action = { type: 'SET_USER', payload: { name: 'John Doe', email: 'john.doe@example.com' } };
const state = userReducer(initialState, action);
expect(state).toHaveProperty('name', 'John Doe');
expect(state).toHaveProperty('email', 'john.doe@example.com');
});
});

0 comments on commit 65a5814

Please sign in to comment.