Skip to content

Commit

Permalink
feat: 1.修改单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
JuckZ committed Jul 5, 2023
1 parent a5f5650 commit 410a50f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
13 changes: 4 additions & 9 deletions src/__test__/weather.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { describe, expect, it } from 'vitest';
import { getWeather } from '@/api/weather';

const utils = {
getWeather: apiKey => {
return getWeather({
apiKey: apiKey || 'e6d27287b8d54b5da382f19086dac223',
});
},
};
const apiKey = 'e6d27287b8d54b5da382f19086dac223';
describe('weather', () => {
it('getWeather', () => {
const weather = utils.getWeather(null);
it('getWeather', async () => {
const weather = await getWeather({ apiKey });
console.log(weather);
expect(weather).not.null;
expect(weather?.textDay).string;
});
});
27 changes: 8 additions & 19 deletions src/api/weather.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { request } from '@/utils/request';

// 你的和风天气API密钥
let API_KEY = '<你的和风天气API密钥>';

Expand Down Expand Up @@ -237,13 +235,10 @@ export async function getWeatherDaily(latitude: number, longitude: number): Prom
try {
// now or 3d
const url = `https://devapi.qweather.com/v7/weather/3d?location=${longitude},${latitude}&key=${API_KEY}`;
console.log(url);

const response = await request({
url,
const response = await fetch(url, {
method: 'GET',
});
const data = JSON.parse(response);
const data = await response.json();
if (data.code != '200') {
return null;
}
Expand All @@ -259,12 +254,10 @@ export async function getWeather({ apiKey }): Promise<WeatherResponse | null> {
try {
API_KEY = 'e6d27287b8d54b5da382f19086dac223';
const position = await getCurrentLocation();
console.log(position);
if (position === null) {
return null;
}
const weather = await getWeatherDaily(position.coords.latitude, position.coords.longitude);
console.log(weather);
return weather;
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -300,12 +293,11 @@ export async function getCurrentLocation(): Promise<{
async function getair(locationId, key) {
const weatherUrl = `https://devapi.qweather.com/v7/air/now?location=${locationId}&key=${key}`;
const wUrl = new URL(weatherUrl);
const res = await request({
url: wUrl.href,
const res = await fetch(wUrl.href, {
method: 'GET',
});

const data = JSON.parse(res);
const data = await res.json();
if (data.code != '200') {
return -1;
}
Expand All @@ -315,23 +307,21 @@ async function getair(locationId, key) {

//查询位置
async function getpos() {
const res = await request({
url: 'https://whois.pconline.com.cn/ipJson.jsp?json=true',
const res = await fetch('https://whois.pconline.com.cn/ipJson.jsp?json=true', {
method: 'GET',
});
const resultObj = JSON.parse(res) as { city: string; cityCode: string };
const resultObj = (await res.json()) as { city: string; cityCode: string };
return resultObj;
}

//查询城市ID
async function searchCity(city) {
const searchUrl = `https://geoapi.qweather.com/v2/city/lookup?location=${city}&key=${API_KEY}&number=1`;
const sUrl = new URL(searchUrl);
const res = await request({
url: sUrl.href,
const res = await fetch(sUrl.href, {
method: 'GET',
});
const data = JSON.parse(res) as {
const data = (await res.json()) as {
code: string;
location: {
id: string;
Expand All @@ -345,7 +335,6 @@ async function searchCity(city) {
lon: string;
}[];
};
console.log(res);
if (data.code == '200') {
const location = data.location[0];
city = location.name;
Expand Down

0 comments on commit 410a50f

Please sign in to comment.