From e50f8294d50cba4f49197300ca637413616ffccd Mon Sep 17 00:00:00 2001 From: clement Date: Sat, 4 May 2024 04:26:31 +0300 Subject: [PATCH] Add tests for chart options. --- src/options.rs | 106 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 8 deletions(-) diff --git a/src/options.rs b/src/options.rs index 142c9ba..b53f401 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,5 +1,5 @@ use std::fmt::Display; -use serde_json::Value; +use serde_json::{to_string, to_string_pretty, Value}; use wasm_bindgen::JsValue; /// Represents the options that can be passed to the ApexCharts constructor. This is a wrapper around @@ -23,10 +23,36 @@ use wasm_bindgen::JsValue; /// } /// ] /// }"#; -/// let options = apexcharts_rs::ChartOptions(String::from(options_str)); -pub struct ChartOptions(String); +/// let options = apexcharts_rs::ChartOptions::from_string(String::from(options_str)); +/// ``` +#[derive(Clone, Debug)] +pub struct ChartOptions { + options: String +} impl ChartOptions { + /// Create a new instance of ChartOptions from a string. + /// ```rust + /// let options_str = r#" + /// { + /// "chart": { + /// "type": "line" + /// }, + /// "series": [ + /// { + /// "name": "Series 1", + /// "data": [30, 40, 35, 50, 49, 125] + /// } + /// ] + /// }"#; + /// let options = apexcharts_rs::ChartOptions::from_string(options_str); + /// let chart = apexcharts_rs::ApexChart::new(&options.into()); + /// ``` + pub fn from_string(options: String) -> Self { + Self { + options + } + } /// Create a new instance of ChartOptions from a json file. /// ```rust /// let options = apexcharts_rs::ChartOptions::from_file("path/to/options.json"); @@ -34,22 +60,32 @@ impl ChartOptions { /// ``` pub fn from_file(file_path: &str) -> Self { // Read the file and return the content as a string - let content = std::fs::read_to_string(file_path).unwrap(); - Self(content) + let options = std::fs::read_to_string(file_path).unwrap(); + Self { + options + } } /// Set the type of the chart. This overrides the type set in the options. pub fn set_chart_type(&self, chart_type: ChartType) -> Self { let chart_type = chart_type.to_string(); - let mut content: Value = serde_json::from_str(&self.0).unwrap(); + let mut content: Value = serde_json::from_str(&self.options).unwrap(); content["chart"]["type"] = Value::String(chart_type); - Self(serde_json::to_string_pretty(&content).unwrap()) + Self { + options: to_string_pretty(&content).unwrap() + } } } impl From for JsValue { fn from(options: ChartOptions) -> JsValue { - JsValue::from_str(&options.0) + JsValue::from_str(&options.options) + } +} + +impl Display for ChartOptions { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.options) } } @@ -97,3 +133,57 @@ impl Display for ChartType { } } } + + +#[cfg(test)] +mod tests { + use serde_json::Value; + use crate::{ChartOptions}; + + const OPTIONS_STR: &str = r#" + { + "chart": { + "type": "line" + }, + "series": [ + { + "data": [30, 40, 35, 50, 49, 125], + "name": "Series 1" + } + ] + }"#; + + #[test] + pub fn test_parsing_chart_options() { + let options = ChartOptions::from_string(String::from(OPTIONS_STR)); + assert_eq!(options.options, OPTIONS_STR); + } + + #[test] + pub fn test_setting_chart_type() { + let options = ChartOptions::from_string(String::from(OPTIONS_STR)) + .set_chart_type(crate::ChartType::Bar); + let new_options = serde_json::from_str::(r#" + { + "chart": { + "type": "bar" + }, + "series": [ + { + "data": [30, 40, 35, 50, 49, 125], + "name": "Series 1" + } + ] + }"#).unwrap(); + assert_eq!(serde_json::from_str::(options.options.as_str()).unwrap(), new_options); + } + + #[test] + pub fn test_parsing_chart_options_from_file() { + let file_path = "options.json"; + std::fs::write(file_path, OPTIONS_STR).unwrap(); + let options = ChartOptions::from_file(file_path); + assert_eq!(options.options, OPTIONS_STR); + std::fs::remove_file(file_path).unwrap(); + } +} \ No newline at end of file