forked from Qortal/q-blog
65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import reducer, {
|
|
addPosts,
|
|
removePost,
|
|
addToHashMap,
|
|
upsertPosts,
|
|
upsertPostsBeginning,
|
|
populateFavorites,
|
|
setCountNewPosts,
|
|
BlogPost,
|
|
} from '@/state/features/blogSlice';
|
|
|
|
const post = (id: string, user = 'alice'): BlogPost => ({
|
|
id,
|
|
user,
|
|
title: 't',
|
|
description: 'd',
|
|
createdAt: 0,
|
|
});
|
|
|
|
describe('blogSlice reducers', () => {
|
|
it('addPosts replaces posts', () => {
|
|
const s = reducer(undefined, addPosts([post('1')]));
|
|
expect(s.posts.map((p) => p.id)).toEqual(['1']);
|
|
});
|
|
|
|
it('removePost removes by id from posts and filteredPosts', () => {
|
|
const init = reducer(undefined, addPosts([post('1'), post('2')]));
|
|
const withFiltered = { ...init, filteredPosts: [post('2'), post('3')] };
|
|
const s = reducer(withFiltered, removePost('2'));
|
|
expect(s.posts.map((p) => p.id)).toEqual(['1']);
|
|
expect(s.filteredPosts.map((p) => p.id)).toEqual(['3']);
|
|
});
|
|
|
|
it('addToHashMap stores key as id-user', () => {
|
|
const s = reducer(undefined, addToHashMap(post('1', 'bob')));
|
|
expect(Object.keys(s.hashMapPosts)).toEqual(['1-bob']);
|
|
});
|
|
|
|
it('upsertPosts updates or inserts', () => {
|
|
const a = reducer(undefined, addPosts([post('1'), post('2')]));
|
|
const b = reducer(a, upsertPosts([post('2', 'x'), post('3')]));
|
|
expect(b.posts.find((p) => p.id === '2')?.user).toBe('x');
|
|
expect(b.posts.some((p) => p.id === '3')).toBe(true);
|
|
});
|
|
|
|
it('upsertPostsBeginning unshifts new and updates existing', () => {
|
|
const a = reducer(undefined, addPosts([post('2')]));
|
|
const b = reducer(a, upsertPostsBeginning([post('1'), post('2', 'y')]));
|
|
expect(b.posts.map((p) => p.id)).toEqual(['1', '2']);
|
|
expect(b.posts.find((p) => p.id === '2')?.user).toBe('y');
|
|
});
|
|
|
|
it('populateFavorites fills favorites without duplicates', () => {
|
|
const a = reducer(undefined, populateFavorites([post('1')]));
|
|
const b = reducer(a, populateFavorites([post('1'), post('2')]));
|
|
expect(b.favorites.map((p) => p.id)).toEqual(['1', '2']);
|
|
});
|
|
|
|
it('setCountNewPosts sets counter', () => {
|
|
const s = reducer(undefined, setCountNewPosts(5));
|
|
expect(s.countNewPosts).toBe(5);
|
|
});
|
|
});
|