File size: 6,885 Bytes
3c29fcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import React from 'react';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import KeywordTrendAnalyzer from '../KeywordTrendAnalyzer';
import postService from '../../services/postService';

// Mock the postService
jest.mock('../../services/postService');

describe('KeywordTrendAnalyzer', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  test('renders the component with initial elements', () => {
    render(<KeywordTrendAnalyzer />);
    
    // Check if the main heading is present
    expect(screen.getByText('Keyword Trend Analysis')).toBeInTheDocument();
    
    // Check if the input field is present
    expect(screen.getByPlaceholderText('Enter keyword to analyze')).toBeInTheDocument();
    
    // Check if the analyze button is present
    expect(screen.getByText('Analyze')).toBeInTheDocument();
  });

  test('shows error when analyzing empty keyword', async () => {
    render(<KeywordTrendAnalyzer />);
    
    // Click the analyze button without entering a keyword
    const analyzeButton = screen.getByText('Analyze');
    fireEvent.click(analyzeButton);
    
    // Wait for the error message to appear
    await waitFor(() => {
      expect(screen.getByText('Please enter a keyword')).toBeInTheDocument();
    });
  });

  test('calls postService with correct keyword when analyzing', async () => {
    // Mock the analyzeKeywordTrend method to return sample data
    const mockAnalysisData = [
      { date: '2024-01-01', daily: 5, weekly: 25, monthly: 100 },
      { date: '2024-01-08', daily: 7, weekly: 30, monthly: 110 }
    ];
    postService.analyzeKeywordTrend.mockResolvedValue({
      data: {
        success: true,
        keyword: 'technology',
        date_range: 'monthly',
        analysis: mockAnalysisData
      }
    });
    
    render(<KeywordTrendAnalyzer />);
    
    // Enter a keyword in the input field
    const keywordInput = screen.getByPlaceholderText('Enter keyword to analyze');
    fireEvent.change(keywordInput, { target: { value: 'technology' } });
    
    // Click the analyze button
    const analyzeButton = screen.getByText('Analyze');
    fireEvent.click(analyzeButton);
    
    // Wait for the analysis to complete
    await waitFor(() => {
      expect(postService.analyzeKeywordTrend).toHaveBeenCalledWith('technology');
    });
  });

  test('displays analysis results when successful', async () => {
    // Mock the analyzeKeywordTrend method to return sample data
    const mockAnalysisData = [
      { date: '2024-01-01', daily: 5, weekly: 25, monthly: 100 },
      { date: '2024-01-08', daily: 7, weekly: 30, monthly: 110 }
    ];
    postService.analyzeKeywordTrend.mockResolvedValue({
      data: {
        success: true,
        keyword: 'technology',
        date_range: 'monthly',
        analysis: mockAnalysisData
      }
    });
    
    render(<KeywordTrendAnalyzer />);
    
    // Enter a keyword in the input field
    const keywordInput = screen.getByPlaceholderText('Enter keyword to analyze');
    fireEvent.change(keywordInput, { target: { value: 'technology' } });
    
    // Click the analyze button
    const analyzeButton = screen.getByText('Analyze');
    fireEvent.click(analyzeButton);
    
    // Wait for the results to be displayed
    await waitFor(() => {
      expect(screen.getByText('Keyword Frequency Trends for "technology"')).toBeInTheDocument();
    });
    
    // Check if the chart container is present
    expect(screen.getByTestId('recharts-responsive-container')).toBeInTheDocument();
    
    // Check if the average stats are displayed
    expect(screen.getByText('Daily Average')).toBeInTheDocument();
    expect(screen.getByText('Weekly Average')).toBeInTheDocument();
    expect(screen.getByText('Monthly Average')).toBeInTheDocument();
  });

  test('shows error message when analysis fails', async () => {
    // Mock the analyzeKeywordTrend method to throw an error
    postService.analyzeKeywordTrend.mockRejectedValue(new Error('Analysis failed'));
    
    render(<KeywordTrendAnalyzer />);
    
    // Enter a keyword in the input field
    const keywordInput = screen.getByPlaceholderText('Enter keyword to analyze');
    fireEvent.change(keywordInput, { target: { value: 'technology' } });
    
    // Click the analyze button
    const analyzeButton = screen.getByText('Analyze');
    fireEvent.click(analyzeButton);
    
    // Wait for the error message to appear
    await waitFor(() => {
      expect(screen.getByText('Failed to analyze keyword. Please try again.')).toBeInTheDocument();
    });
  });

  test('shows loading state during analysis', async () => {
    // Create a promise that doesn't resolve immediately to simulate loading
    const mockPromise = new Promise((resolve) => {
      setTimeout(() => resolve({
        data: {
          success: true,
          keyword: 'technology',
          date_range: 'monthly',
          analysis: [{ date: '2024-01-01', daily: 5, weekly: 25, monthly: 100 }]
        }
      }), 100);
    });
    
    postService.analyzeKeywordTrend.mockReturnValue(mockPromise);
    
    render(<KeywordTrendAnalyzer />);
    
    // Enter a keyword in the input field
    const keywordInput = screen.getByPlaceholderText('Enter keyword to analyze');
    fireEvent.change(keywordInput, { target: { value: 'technology' } });
    
    // Click the analyze button
    const analyzeButton = screen.getByText('Analyze');
    fireEvent.click(analyzeButton);
    
    // Check if the button text changes to 'Analyzing...'
    expect(analyzeButton).toHaveTextContent('Analyzing...');
    
    // Wait for the analysis to complete
    await waitFor(() => {
      expect(analyzeButton).toHaveTextContent('Analyze');
    });
  });

  test('disables analyze button during loading', async () => {
    // Create a promise that doesn't resolve immediately to simulate loading
    const mockPromise = new Promise((resolve) => {
      setTimeout(() => resolve({
        data: {
          success: true,
          keyword: 'technology',
          date_range: 'monthly',
          analysis: [{ date: '2024-01-01', daily: 5, weekly: 25, monthly: 100 }]
        }
      }), 100);
    });
    
    postService.analyzeKeywordTrend.mockReturnValue(mockPromise);
    
    render(<KeywordTrendAnalyzer />);
    
    // Enter a keyword in the input field
    const keywordInput = screen.getByPlaceholderText('Enter keyword to analyze');
    fireEvent.change(keywordInput, { target: { value: 'technology' } });
    
    // Click the analyze button
    const analyzeButton = screen.getByText('Analyze');
    fireEvent.click(analyzeButton);
    
    // Check if the button is disabled
    expect(analyzeButton).toBeDisabled();
    
    // Wait for the analysis to complete
    await waitFor(() => {
      expect(analyzeButton).not.toBeDisabled();
    });
  });
});